go: add «2537. Count the Number of Good Subarrays»
URL: https://leetcode.com/problems/count-the-number-of-good-subarrays/ Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
02a57cd6f3
commit
38cea1225d
1 changed files with 24 additions and 0 deletions
24
go/count-the-number-of-good-subarrays.go
Normal file
24
go/count-the-number-of-good-subarrays.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue