URL: https://leetcode.com/problems/count-the-number-of-good-subarrays/ Signed-off-by: Matej Focko <me@mfocko.xyz>
24 lines
388 B
Go
24 lines
388 B
Go
package main
|
|
|
|
func countGood(nums []int, k int) int64 {
|
|
counters := make(map[int]int)
|
|
count := int64(0)
|
|
|
|
same, right := 0, -1
|
|
for _, x := range nums {
|
|
for same < k && right+1 < len(nums) {
|
|
right++
|
|
same += counters[nums[right]]
|
|
counters[nums[right]]++
|
|
}
|
|
|
|
if same >= k {
|
|
count += int64(len(nums) - right)
|
|
}
|
|
|
|
counters[x]--
|
|
same -= counters[x]
|
|
}
|
|
|
|
return count
|
|
}
|