1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-14 01:49:41 +01:00
LeetCode/go/count-number-of-nice-subarrays.go
Matej Focko c87d176475
go: add «1248. Count Number of Nice Subarrays»
Signed-off-by: Matej Focko <me@mfocko.xyz>
2024-06-22 19:42:32 +02:00

23 lines
349 B
Go

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