{"id":4918,"date":"2019-03-02T20:18:59","date_gmt":"2019-03-03T04:18:59","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4918"},"modified":"2019-03-02T20:19:45","modified_gmt":"2019-03-03T04:19:45","slug":"leetcode-1004-max-consecutive-ones-iii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/two-pointers\/leetcode-1004-max-consecutive-ones-iii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1004. Max Consecutive Ones III"},"content":{"rendered":"\n<p>Given an array&nbsp;<code>A<\/code>&nbsp;of 0s and 1s, we may change up to&nbsp;<code>K<\/code>&nbsp;values from 0 to 1.<\/p>\n\n\n\n<p>Return the length of the longest (contiguous) subarray that contains only 1s.&nbsp;<\/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 = [1,1,1,0,0,0,1,1,1,1,0], K = 2\n<strong>Output: <\/strong>6\n<strong>Explanation: <\/strong>\n[1,1,1,0,0,<strong>1<\/strong>,1,1,1,1,<strong>1<\/strong>]\nBolded numbers were flipped from 0 to 1.  The longest subarray is underlined.<\/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 = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3\n<strong>Output: <\/strong>10\n<strong>Explanation: <\/strong>\n[0,0,1,1,<strong>1<\/strong>,<strong>1<\/strong>,1,1,1,<strong>1<\/strong>,1,1,0,0,0,1,1,1,1]\nBolded numbers were flipped from 0 to 1.  The longest subarray is underlined.\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;= 20000<\/code><\/li><li><code>0 &lt;= K &lt;= A.length<\/code><\/li><li><code>A[i]<\/code>&nbsp;is&nbsp;<code>0<\/code>&nbsp;or&nbsp;<code>1<\/code>&nbsp;<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution : Sliding Window<\/strong><\/h2>\n\n\n\n<p>Maintain a window that has at most K zeros<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int longestOnes(vector<int>& A, int K) {\n    int l = 0;\n    int zeros = 0;\n    int ans = 0;\n    for (int r = 0; r < A.size(); ++r) {\n      if (A[r] == 0) ++zeros;\n      while (zeros > K)\n        if (A[l++] == 0) --zeros;\n      ans = max(ans, r - l + 1);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given an array&nbsp;A&nbsp;of 0s and 1s, we may change up to&nbsp;K&nbsp;values from 0 to 1. Return the length of the longest (contiguous) subarray that contains&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[176],"tags":[215],"class_list":["post-4918","post","type-post","status-publish","format-standard","hentry","category-two-pointers","tag-sliding-window","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4918","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=4918"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4918\/revisions"}],"predecessor-version":[{"id":4921,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4918\/revisions\/4921"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4918"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4918"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4918"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}