go: add «995. Minimum Number of K Consecutive Bit Flips»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-06-24 16:52:40 +02:00
parent 24735bf4c0
commit 6c0af823be
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -0,0 +1,33 @@
package main
import (
arrayqueue "github.com/emirpasic/gods/v2/queues/arrayqueue"
)
func minKBitFlips(bits []int, k int) int {
q := arrayqueue.New[int]()
total := 0
flipped := 0
for i, bit := range bits {
if i >= k {
b, _ := q.Dequeue()
flipped ^= b
}
if flipped != bit {
q.Enqueue(0)
continue
}
if i+k > len(bits) {
return -1
}
q.Enqueue(1)
flipped ^= 1
total++
}
return total
}