go: add «1248. Count Number of Nice Subarrays»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-06-22 19:42:32 +02:00
parent 1a95a0e633
commit c87d176475
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -0,0 +1,23 @@
package main
func numberOfSubarrays(nums []int, k int) int {
atMost := func(k int) int {
subarrays := 0
window, start := 0, 0
for end := range nums {
window += nums[end] % 2
for window > k {
window -= nums[start] % 2
start++
}
subarrays += end - start + 1
}
return subarrays
}
return atMost(k) - atMost(k-1)
}