diff --git a/leetcode/901-1000/0992.Subarrays-with-K-Different-Integers/README.md b/leetcode/901-1000/0992.Subarrays-with-K-Different-Integers/README.md index d820142f4..3220682e2 100644 --- a/leetcode/901-1000/0992.Subarrays-with-K-Different-Integers/README.md +++ b/leetcode/901-1000/0992.Subarrays-with-K-Different-Integers/README.md @@ -1,28 +1,29 @@ # [992.Subarrays with K Different Integers][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 +Given an integer array `nums` and an integer `k`, return the number of **good subarrays** of `nums`. + +A **good array** is an array where the number of different integers in that array is exactly `k`. + +- For example, `[1,2,3,1,2]` has `3` different integers: `1`, `2`, and `3`. + +A **subarray** is a **contiguous** part of an array. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [1,2,1,2,3], k = 2 +Output: 7 +Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2] ``` -## 题意 -> ... +**Example 2: ** -## 题解 - -### 思路1 -> ... -Subarrays with K Different Integers -```go ``` - +Input: nums = [1,2,1,3,4], k = 3 +Output: 3 +Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4]. +``` ## 结语 diff --git a/leetcode/901-1000/0992.Subarrays-with-K-Different-Integers/Solution.go b/leetcode/901-1000/0992.Subarrays-with-K-Different-Integers/Solution.go index d115ccf5e..922633b92 100644 --- a/leetcode/901-1000/0992.Subarrays-with-K-Different-Integers/Solution.go +++ b/leetcode/901-1000/0992.Subarrays-with-K-Different-Integers/Solution.go @@ -1,5 +1,30 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int, k int) int { + ll := len(nums) + count := make([]int, ll+1) + ans := 0 + l, r := 0, 0 + cc := 0 + for ; r < ll; r++ { + count[nums[r]]++ + if count[nums[r]] == 1 { + k-- + } + if k < 0 { + count[nums[l]]-- + l++ + k++ + cc = 0 + } + if k == 0 { + for count[nums[l]] > 1 { + count[nums[l]]-- + l++ + cc++ + } + ans += (cc + 1) + } + } + return ans } diff --git a/leetcode/901-1000/0992.Subarrays-with-K-Different-Integers/Solution_test.go b/leetcode/901-1000/0992.Subarrays-with-K-Different-Integers/Solution_test.go index 14ff50eb4..70ebf4b38 100644 --- a/leetcode/901-1000/0992.Subarrays-with-K-Different-Integers/Solution_test.go +++ b/leetcode/901-1000/0992.Subarrays-with-K-Different-Integers/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + k int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, 2, 1, 2, 3}, 2, 7}, + {"TestCase2", []int{1, 2, 1, 3, 4}, 3, 3}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.inputs, 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", + c.expect, got, c.inputs, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }