Skip to content

Commit 721e107

Browse files
authored
Update 995-minimum-number-of-k-consecutive-bit-flips.js
1 parent 5b4cfd5 commit 721e107

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

995-minimum-number-of-k-consecutive-bit-flips.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
const minKBitFlips = function(nums, k) {
7+
let cur = 0, res = 0
8+
const n = nums.length
9+
for(let i = 0; i < n; i++) {
10+
if(i >= k && nums[i - k] === 2) cur--
11+
if(cur % 2 === nums[i]) {
12+
if(i + k > n) return -1
13+
nums[i] = 2
14+
cur++
15+
res++
16+
}
17+
}
18+
return res
19+
};
20+
21+
// another
22+
123
/**
224
* @param {number[]} A
325
* @param {number} K

0 commit comments

Comments
 (0)