go: add «1248. Count Number of Nice Subarrays»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
1a95a0e633
commit
c87d176475
1 changed files with 23 additions and 0 deletions
23
go/count-number-of-nice-subarrays.go
Normal file
23
go/count-number-of-nice-subarrays.go
Normal 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)
|
||||
}
|
Loading…
Reference in a new issue