From 6c0af823be80e03740efdb4ea18f4072354a63b5 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Mon, 24 Jun 2024 16:52:40 +0200 Subject: [PATCH] =?UTF-8?q?go:=20add=20=C2=AB995.=20Minimum=20Number=20of?= =?UTF-8?q?=20K=20Consecutive=20Bit=20Flips=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- ...nimum-number-of-k-consecutive-bit-flips.go | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 go/minimum-number-of-k-consecutive-bit-flips.go diff --git a/go/minimum-number-of-k-consecutive-bit-flips.go b/go/minimum-number-of-k-consecutive-bit-flips.go new file mode 100644 index 0000000..26ea51e --- /dev/null +++ b/go/minimum-number-of-k-consecutive-bit-flips.go @@ -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 +}