Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 220 #949

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions leetcode/201-300/0220.Contains-Duplicate-III/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
# [220.Contains Duplicate III][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
You are given an integer array `nums` and two integers `indexDiff` and `valueDiff`.

Find a pair of indices `(i, j)` such that:

- `i != j`,
- `abs(i - j) <= indexDiff`.
- `abs(nums[i] - nums[j]) <= valueDiff`, and

Return `true` if such pair exists or `false` otherwise.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: nums = [1,2,3,1], indexDiff = 3, valueDiff = 0
Output: true
Explanation: We can choose (i, j) = (0, 3).
We satisfy the three conditions:
i != j --> 0 != 3
abs(i - j) <= indexDiff --> abs(0 - 3) <= 3
abs(nums[i] - nums[j]) <= valueDiff --> abs(1 - 1) <= 0
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Contains Duplicate III
```go
```

Input: nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3
Output: false
Explanation: After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false.
```

## 结语

Expand Down
34 changes: 32 additions & 2 deletions leetcode/201-300/0220.Contains-Duplicate-III/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
package Solution

func Solution(x bool) bool {
return x
import "sort"

func Solution(nums []int, indexDiff int, valueDiff int) bool {
indies := make([]int, len(nums))
for i := range nums {
indies[i] = i
}
sort.Slice(indies, func(i, j int) bool {
a, b := nums[indies[i]], nums[indies[j]]
if a == b {
return indies[i] < indies[j]
}
return a < b
})

start, end := 0, 1
for ; end < len(nums); end++ {
v := nums[indies[end]] - nums[indies[start]]
if v > valueDiff {
start++
end = start
continue
}
i := indies[end] - indies[start]
if i < 0 {
i = -i
}
if i <= indexDiff {
return true
}
}
return false
}
18 changes: 9 additions & 9 deletions leetcode/201-300/0220.Contains-Duplicate-III/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
nums []int
a, b int
expect bool
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1, 2, 3, 1}, 3, 0, true},
{"TestCase2", []int{1, 5, 9, 1, 5, 9}, 2, 3, false},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.nums, c.a, c.b)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
c.expect, got, c.nums, c.a, c.b)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading