|
| 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