Leikou brushing notes: 995.K minimum number of consecutive bit flips (sliding window method, use queue to record flips to reduce time complexity, detailed problem solutions)

topic:

995, K minimum number of consecutive bit flips

In array A containing only 0 and 1, a K bit flip involves selecting a (contiguous) sub-array of length K, and changing each 0 in the sub-array to 1 and changing each 1 to 0.

Returns the minimum number of K bit flips required so that the array has no elements with value 0. If it is not possible, return -1.

Example 1:

Input: A = [0,1,0], K = 1
Output: 2
Explanation: First flip A[0], then flip A[2].

Example 2:

Input: A = [1,1,0], K = 2
Output: -1
Explanation: No matter how we flip the sub-array of size 2, we cannot make the array [1,1,1].

Example 3:

Input: A = [0,0,0,1,0,1,1,0], K = 3
Output: 3
Explanation:
Flip A[0],A[1],A[2]: A becomes [ 1,1,1,1,0,1,1,0]
flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0, 0]
Flip A[5], A[6], A[7]: A becomes [1,1,1,1,1,1,1,1]

prompt:

1 <= A.length <= 30000
1 <= K <= A.length

Problem solution ideas:

The main idea of ​​the topic: each time you flip the sub-array of length K, find the minimum number of flips so that all 0s in the array are changed to 1. If it cannot be achieved, it returns -1.

Conclusion 1 : The flip of the following interval will not affect the previous elements. Therefore, you can use the greedy strategy, traverse from left to right, and turn it over with the following K numbers when you encounter each 0.
Conclusion 2 : The result of flipping A[i] even times is A[i]; the result of flipping odd times is A[i] ^ 1.

At this time, it is easy to think of a real flip method with a time complexity of O(nk), but this method will time out. The main reason for the timeout is that we actually performed the flip. According to conclusion 2, the current state of position i is related to the number of times it is flipped by the previous K-1 elements (parity).

We use a queue to simulate a sliding window. The meaning of the sliding window is the subinterval starting at which position among the previous K-1 elements has been flipped. The sliding window slides from left to right. If the current position i needs to be flipped, the position is stored in the queue. When traversing to the new position j (j <i + K), the number of elements in the queue represents the number of times i is flipped by the previous K-1 elements.

When the position i is flipped an even number of times, if A[i] is 0, then it is still 0 after flipping, and the current element needs to be flipped;
when the position of i is flipped an odd number of times, if A[i] is 1, then the result is 0, the current element needs to be flipped.
Combining the above two points, we get a conclusion, if len(que)% 2 == A[i], the current element needs to be flipped.

When i + K> N, it means that the subinterval of size K needs to be flipped, but there are less than K remaining elements, so -1 is returned.

Time complexity: O(N).
Space complexity: O(K).

Problem solution python code:

class Solution(object):
    def minKBitFlips(self, A, K):
        """
        :type A: List[int]
        :type K: int
        :rtype: int
        """
        N = len(A)
        que = collections.deque()
        res = 0
        for i in range(N):
            if que and i >= que[0] + K:
                que.popleft()
            if len(que) % 2 == A[i]:
                if i +  K > N: return -1
                que.append(i)
                res += 1
        return res

Author: fuxuemingzhu
link: https://leetcode-cn.com/problems/minimum-number-of-k-consecutive-bit-flips/solution/hua-dong-chuang-kou-shi-ben-ti-zui-rong -z403l/
Source: LeetCode https://leetcode-cn.com/problems/minimum-number-of-k-consecutive-bit-flips/

Guess you like

Origin blog.csdn.net/weixin_44414948/article/details/113861118