Minimum Number of K Consecutive Bit Flips LeetCode solution in Java

1
class Solution {
2
public int minKBitFlips(int[] nums, int k) {
3
int target = 0, ans = 0;
4
;
5
boolean[] flip = new boolean[nums.length + 1];
6
for (int i = 0; i < nums.length; i++) {
7
if (flip[i]) {
8
target ^= 1;
9
}
10
if (i < nums.length - k + 1 && nums[i] == target) {
11
target ^= 1;
12
flip[i + k] ^= true;
13
ans++;
14
}
15
if (i > nums.length - k && nums[i] == target) {
16
return -1;
17
}
18
}
19
return ans;
20
}
21
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0