mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-14 01:49:41 +01:00
34 lines
427 B
Go
34 lines
427 B
Go
|
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
|
||
|
}
|