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:
Matej Focko 2025-04-16 09:55:54 +02:00
parent 02a57cd6f3
commit 38cea1225d
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View 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
}