diff --git a/leetcode/2101-2200/2200.Find-All-K-Distant-Indices-in-an-Array/README.md b/leetcode/2101-2200/2200.Find-All-K-Distant-Indices-in-an-Array/README.md index 145b4c2ad..6c7aa6afe 100755 --- a/leetcode/2101-2200/2200.Find-All-K-Distant-Indices-in-an-Array/README.md +++ b/leetcode/2101-2200/2200.Find-All-K-Distant-Indices-in-an-Array/README.md @@ -1,28 +1,34 @@ # [2200.Find All K-Distant Indices in an Array][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 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`. + +Return a list of all k-distant indices sorted in **increasing order**. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [3,4,9,1,3,9,5], key = 9, k = 1 +Output: [1,2,3,4,5,6] +Explanation: Here, nums[2] == key and nums[5] == key. +- 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. +- For index 1, |1 - 2| <= k and nums[2] == key, so 1 is a k-distant index. +- For index 2, |2 - 2| <= k and nums[2] == key, so 2 is a k-distant index. +- For index 3, |3 - 2| <= k and nums[2] == key, so 3 is a k-distant index. +- For index 4, |4 - 5| <= k and nums[5] == key, so 4 is a k-distant index. +- For index 5, |5 - 5| <= k and nums[5] == key, so 5 is a k-distant index. +- For index 6, |6 - 5| <= k and nums[5] == key, so 6 is a k-distant index. +Thus, we return [1,2,3,4,5,6] which is sorted in increasing order. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Find All K-Distant Indices in an Array -```go ``` - +Input: nums = [2,2,2,2,2], key = 2, k = 2 +Output: [0,1,2,3,4] +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. +Hence, we return [0,1,2,3,4]. +``` ## 结语 diff --git a/leetcode/2101-2200/2200.Find-All-K-Distant-Indices-in-an-Array/Solution.go b/leetcode/2101-2200/2200.Find-All-K-Distant-Indices-in-an-Array/Solution.go index d115ccf5e..e9f6f314b 100644 --- a/leetcode/2101-2200/2200.Find-All-K-Distant-Indices-in-an-Array/Solution.go +++ b/leetcode/2101-2200/2200.Find-All-K-Distant-Indices-in-an-Array/Solution.go @@ -1,5 +1,45 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int, key int, k int) []int { + l := len(nums) + used := make([]bool, l) + c := 0 + for i := 0; i < l; i++ { + if c > 0 { + used[i] = true + if nums[i] == key { + c = k + } else { + c-- + } + continue + } + if nums[i] == key { + c = k + used[i] = true + } + } + c = 0 + for i := l - 1; i >= 0; i-- { + if c > 0 { + used[i] = true + if nums[i] == key { + c = k + } else { + c-- + } + continue + } + if nums[i] == key { + c = k + used[i] = true + } + } + var ans []int + for i := range used { + if used[i] { + ans = append(ans, i) + } + } + return ans } diff --git a/leetcode/2101-2200/2200.Find-All-K-Distant-Indices-in-an-Array/Solution_test.go b/leetcode/2101-2200/2200.Find-All-K-Distant-Indices-in-an-Array/Solution_test.go index 14ff50eb4..52c280ae3 100644 --- a/leetcode/2101-2200/2200.Find-All-K-Distant-Indices-in-an-Array/Solution_test.go +++ b/leetcode/2101-2200/2200.Find-All-K-Distant-Indices-in-an-Array/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + nums []int + key, k int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{3, 4, 9, 1, 3, 9, 5}, 9, 1, []int{1, 2, 3, 4, 5, 6}}, + {"TestCase2", []int{2, 2, 2, 2, 2}, 2, 2, []int{0, 1, 2, 3, 4}}, } // 开始测试 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.key, c.k) 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.key, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }