go: add «995. Minimum Number of K Consecutive Bit Flips»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
24735bf4c0
commit
6c0af823be
1 changed files with 33 additions and 0 deletions
33
go/minimum-number-of-k-consecutive-bit-flips.go
Normal file
33
go/minimum-number-of-k-consecutive-bit-flips.go
Normal 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
|
||||
}
|
Loading…
Reference in a new issue