Skip to content

Commit 494d6ed

Browse files
authored
Merge pull request 6boris#1243 from 0xff-dev/2200
Add solution and test-cases for problem 2200
2 parents 98a01ce + b069561 commit 494d6ed

File tree

3 files changed

+72
-26
lines changed

3 files changed

+72
-26
lines changed

leetcode/2101-2200/2200.Find-All-K-Distant-Indices-in-an-Array/README.md

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
# [2200.Find All K-Distant Indices in an Array][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given a **0-indexed** integer array `nums` and two integers `key` and `k`. A **k-distant index** is an index `i` of `nums` for which there exists at least one index `j` such that `|i - j| <= k` and `nums[j] == key`.
5+
6+
Return a list of all k-distant indices sorted in **increasing order**.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: nums = [3,4,9,1,3,9,5], key = 9, k = 1
12+
Output: [1,2,3,4,5,6]
13+
Explanation: Here, nums[2] == key and nums[5] == key.
14+
- For index 0, |0 - 2| > k and |0 - 5| > k, so there is no j where |0 - j| <= k and nums[j] == key. Thus, 0 is not a k-distant index.
15+
- For index 1, |1 - 2| <= k and nums[2] == key, so 1 is a k-distant index.
16+
- For index 2, |2 - 2| <= k and nums[2] == key, so 2 is a k-distant index.
17+
- For index 3, |3 - 2| <= k and nums[2] == key, so 3 is a k-distant index.
18+
- For index 4, |4 - 5| <= k and nums[5] == key, so 4 is a k-distant index.
19+
- For index 5, |5 - 5| <= k and nums[5] == key, so 5 is a k-distant index.
20+
- For index 6, |6 - 5| <= k and nums[5] == key, so 6 is a k-distant index.
21+
Thus, we return [1,2,3,4,5,6] which is sorted in increasing order.
1322
```
1423

15-
## 题意
16-
> ...
17-
18-
## 题解
24+
**Example 2:**
1925

20-
### 思路1
21-
> ...
22-
Find All K-Distant Indices in an Array
23-
```go
2426
```
25-
27+
Input: nums = [2,2,2,2,2], key = 2, k = 2
28+
Output: [0,1,2,3,4]
29+
Explanation: For all indices i in nums, there exists some index j such that |i - j| <= k and nums[j] == key, so every index is a k-distant index.
30+
Hence, we return [0,1,2,3,4].
31+
```
2632

2733
## 结语
2834

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,45 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int, key int, k int) []int {
4+
l := len(nums)
5+
used := make([]bool, l)
6+
c := 0
7+
for i := 0; i < l; i++ {
8+
if c > 0 {
9+
used[i] = true
10+
if nums[i] == key {
11+
c = k
12+
} else {
13+
c--
14+
}
15+
continue
16+
}
17+
if nums[i] == key {
18+
c = k
19+
used[i] = true
20+
}
21+
}
22+
c = 0
23+
for i := l - 1; i >= 0; i-- {
24+
if c > 0 {
25+
used[i] = true
26+
if nums[i] == key {
27+
c = k
28+
} else {
29+
c--
30+
}
31+
continue
32+
}
33+
if nums[i] == key {
34+
c = k
35+
used[i] = true
36+
}
37+
}
38+
var ans []int
39+
for i := range used {
40+
if used[i] {
41+
ans = append(ans, i)
42+
}
43+
}
44+
return ans
545
}

leetcode/2101-2200/2200.Find-All-K-Distant-Indices-in-an-Array/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
nums []int
14+
key, k int
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{3, 4, 9, 1, 3, 9, 5}, 9, 1, []int{1, 2, 3, 4, 5, 6}},
18+
{"TestCase2", []int{2, 2, 2, 2, 2}, 2, 2, []int{0, 1, 2, 3, 4}},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.nums, c.key, c.k)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
27+
c.expect, got, c.nums, c.key, c.k)
2828
}
2929
})
3030
}
3131
}
3232

33-
//压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
//使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)