@@ -0,0 +1,1804 @@
+package greey;%0A%0Apublic class Q995MinimumNumberOfKConsecutiveBitFlips %7B%0A%0A    //Difficulty: Hard%0A    //TAG: bit operation%0A    //TAG: greedy%0A    //TODO: review%0A%0A    /**%0A     * 995. Minimum Number of K Consecutive Bit Flips%0A     * In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0.%0A     *%0A     * Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.%0A     *%0A     *%0A     *%0A     * Example 1:%0A     *%0A     * Input: A = %5B0,1,0%5D, K = 1%0A     * Output: 2%0A     * Explanation: Flip A%5B0%5D, then flip A%5B2%5D.%0A     * Example 2:%0A     *%0A     * Input: A = %5B1,1,0%5D, K = 2%0A     * Output: -1%0A     * Explanation: No matter how we flip subarrays of size 2, we can't make the array become %5B1,1,1%5D.%0A     * Example 3:%0A     *%0A     * Input: A = %5B0,0,0,1,0,1,1,0%5D, K = 3%0A     * Output: 3%0A     * Explanation:%0A     * Flip A%5B0%5D,A%5B1%5D,A%5B2%5D: A becomes %5B1,1,1,1,0,1,1,0%5D%0A     * Flip A%5B4%5D,A%5B5%5D,A%5B6%5D: A becomes %5B1,1,1,1,1,0,0,0%5D%0A     * Flip A%5B5%5D,A%5B6%5D,A%5B7%5D: A becomes %5B1,1,1,1,1,1,1,1%5D%0A     *%0A     *%0A     * Note:%0A     *%0A     * 1 %3C= A.length %3C= 30000%0A     * 1 %3C= K %3C= A.length%0A     */%0A%0A    /*%0A    Solution:%0A    https://leetcode.com/problems/minimum-number-of-k-consecutive-bit-flips/discuss/238609/JavaC%252B%252BPython-One-Pass-and-O(1)-Space%0A     */%0A%0A    public int minKBitFlips(int%5B%5D A, int K) %7B%0A        int cur = 0, res = 0;%0A        for (int i = 0; i %3C A.length; ++i) %7B%0A            if (i %3E= K) cur -= A%5Bi - K%5D / 2;%0A            if ((cur & 1 %5E A%5Bi%5D) == 0) %7B%0A                if (i + K %3E A.length) return -1;%0A                A%5Bi%5D += 2;%0A                cur++;%0A                res++;%0A            %7D%0A        %7D%0A        return res;%0A    %7D%0A%0A%7D%0A

995. Minimum Number of K Consecutive Bit Flips