Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1de3db6

Browse files
hard: 995. Minimum Number of K Consecutive Bit Flips
1 parent fac8e1e commit 1de3db6

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
function minKBitFlips(nums, k) {
7+
let n = nums.length;
8+
let flipCount = 0; // to count the number of flips
9+
let flipped = new Array(n).fill(0); // track the flips using an auxiliary array
10+
let isFlipped = 0; // track the current state of flips
11+
12+
for (let i = 0; i < n; i++) {
13+
if (i >= k) {
14+
isFlipped ^= flipped[i - k]; // unmark the flip effect of the position that is out of the current window
15+
}
16+
17+
if (nums[i] === isFlipped) { // if the current bit needs to be flipped
18+
if (i + k > n) {
19+
return -1; // not enough elements to flip k bits
20+
}
21+
flipped[i] = 1; // mark the flip
22+
isFlipped ^= 1; // toggle the flip state
23+
flipCount++; // increment the flip count
24+
}
25+
}
26+
27+
return flipCount;
28+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# [995. Minimum Number of K Consecutive Bit Flips](https://leetcode.com/problems/minimum-number-of-k-consecutive-bit-flips)
2+
3+
---
4+
5+
title: "Minimize K Consecutive Bit Flips"
6+
summary: "An approach to solve the problem of minimizing K consecutive bit flips using a greedy algorithm."
7+
date: "2024年06月24日"
8+
modified_date: "2024年06月24日"
9+
tags: ["algorithm", "greedy", "bit manipulation"]
10+
slug: "minimize-k-consecutive-bit-flips"
11+
12+
---
13+
14+
# Intuition
15+
16+
To solve the problem of flipping k consecutive bits to make all bits 1, we need a strategy that allows us to keep track of the flips efficiently. The naive approach of flipping each bit directly would be too slow, so we need a more optimized method.
17+
18+
# Approach
19+
20+
We use a greedy algorithm combined with an auxiliary array to keep track of the flips. The main idea is to iterate through the array and flip the bits as needed. We also use an auxiliary array to record the flip operations and maintain the current flip state using a variable.
21+
22+
Here's a step-by-step approach:
23+
24+
1. Initialize a counter `flipCount` to count the number of flips.
25+
2. Use an auxiliary array `flipped` to keep track of the flips at each position.
26+
3. Iterate through the array, and for each bit, determine if it needs to be flipped based on the current flip state.
27+
4. If a flip is needed, check if it's possible to flip the next `k` bits. If not, return -1.
28+
5. Update the flip state and mark the current position in the auxiliary array.
29+
6. Continue until all bits are processed.
30+
31+
# Complexity
32+
33+
- **Time complexity:** $$O(n)$$
34+
Iterate through the array once, making the time complexity linear with respect to the size of the input array.
35+
36+
- **Space complexity:** $$O(n)$$
37+
Use an auxiliary array of the same size as the input array to keep track of the flips.
38+
39+
# Code
40+
41+
```javascript
42+
/**
43+
* @param {number[]} nums
44+
* @param {number} k
45+
* @return {number}
46+
*/
47+
function minKBitFlips(nums, k) {
48+
let n = nums.length;
49+
let flipCount = 0; // to count the number of flips
50+
let flipped = new Array(n).fill(0); // track the flips using an auxiliary array
51+
let isFlipped = 0; // track the current state of flips
52+
53+
for (let i = 0; i < n; i++) {
54+
if (i >= k) {
55+
isFlipped ^= flipped[i - k]; // unmark the flip effect of the position that is out of the current window
56+
}
57+
58+
if (nums[i] === isFlipped) {
59+
// if the current bit needs to be flipped
60+
if (i + k > n) {
61+
return -1; // not enough elements to flip k bits
62+
}
63+
flipped[i] = 1; // mark the flip
64+
isFlipped ^= 1; // toggle the flip state
65+
flipCount++; // increment the flip count
66+
}
67+
}
68+
69+
return flipCount;
70+
}
71+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /