{"id":4866,"date":"2019-02-17T00:02:02","date_gmt":"2019-02-17T08:02:02","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4866"},"modified":"2019-02-17T00:56:13","modified_gmt":"2019-02-17T08:56:13","slug":"leetcode-995-minimum-number-of-k-consecutive-bit-flips","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-995-minimum-number-of-k-consecutive-bit-flips\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 995. Minimum Number of K Consecutive Bit Flips"},"content":{"rendered":"\n<p>In an array&nbsp;<code>A<\/code>&nbsp;containing only 0s and 1s, a&nbsp;<em><code>K<\/code>-bit flip&nbsp;<\/em>consists of choosing a (contiguous) subarray of length&nbsp;<code>K<\/code>&nbsp;and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0.<\/p>\n\n\n\n<p>Return the minimum number of&nbsp;<code>K<\/code>-bit flips required so that there is no 0 in the array.&nbsp; If it is not possible, return&nbsp;<code>-1<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>A = [0,1,0], K = 1\n<strong>Output: <\/strong>2\n<strong>Explanation: <\/strong>Flip A[0], then flip A[2].\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>A = [1,1,0], K = 2\n<strong>Output: <\/strong>-1\n<strong>Explanation:<\/strong>&nbsp;No matter how we flip subarrays of size 2, we can't make the array become [1,1,1].\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>A = [0,0,0,1,0,1,1,0], K = 3\n<strong>Output: <\/strong>3\n<strong>Explanation:<\/strong>\nFlip A[0],A[1],A[2]:&nbsp;A becomes [1,1,1,1,0,1,1,0]\nFlip A[4],A[5],A[6]:&nbsp;A becomes [1,1,1,1,1,0,0,0]\nFlip A[5],A[6],A[7]:&nbsp;A becomes [1,1,1,1,1,1,1,1]\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>1 &lt;= A.length &lt;=&nbsp;30000<\/code><\/li><li><code>1 &lt;= K &lt;= A.length<\/code><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<p>From left most, if there is a 0, that bit must be flipped since the right ones won&#8217;t affect left ones.<\/p>\n\n\n\n<p>Time complexity: O(nk) -&gt; O(k)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++ \/ O(nk)<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua, running time: 5172 ms, 16.4 MB\nclass Solution {\npublic:\n  int minKBitFlips(vector<int>& A, int K) {\n    int ans = 0;    \n    for (int i = 0; i <= A.size() - K; ++i) {\n      if (A[i] == 1) continue;\n      ++ans;\n      for (int j = 0; j < K; ++j)\n        A[i + j] ^= 1;      \n    }\n    for (int i = A.size() - K + 1; i < A.size(); ++i)\n      if (A[i] == 0) return -1;\n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">C++ \/ O(n)<\/h2>\n<div class=\"tabcontent\">\n\n<pre>\n\/\/ Author: Huahua, running time: 100 ms, 19.6 MB\nclass Solution {\npublic:\n  int minKBitFlips(vector<int>& A, int K) {\n    vector<int> flipped(A.size());\n    int ans = 0;    \n    int flip = 0;\n    for (int i = 0; i < A.size(); ++i) {\n      if (i >= K) flip ^= flipped[i - K];\n      if ((A[i] ^ flip) == 0) {\n        if (i + K > A.size()) return -1;\n        ++ans;\n        flip ^= 1;\n        flipped[i] = 1;\n      }\n    }    \n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In an array&nbsp;A&nbsp;containing only 0s and 1s, a&nbsp;K-bit flip&nbsp;consists of choosing a (contiguous) subarray of length&nbsp;K&nbsp;and simultaneously changing every 0 in the subarray to 1,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[88,217,215],"class_list":["post-4866","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-hard","tag-sliding-window","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4866","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=4866"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4866\/revisions"}],"predecessor-version":[{"id":4870,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4866\/revisions\/4870"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4866"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4866"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}