{"id":4208,"date":"2018-10-20T21:22:56","date_gmt":"2018-10-21T04:22:56","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4208"},"modified":"2018-10-23T20:53:47","modified_gmt":"2018-10-24T03:53:47","slug":"leetcode-926-flip-string-to-monotone-increasing","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-926-flip-string-to-monotone-increasing\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 926. Flip String to Monotone Increasing"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 926. Flip String to Monotone Increasing - \u5237\u9898\u627e\u5de5\u4f5c EP228\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/D8xa8ZMV7AI?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h1><strong>Problem<\/strong><\/h1>\n<p>A string of\u00a0<code>'0'<\/code>s and\u00a0<code>'1'<\/code>s is\u00a0<em>monotone increasing<\/em>\u00a0if it consists of some number of\u00a0<code>'0'<\/code>s (possibly 0), followed by some number of\u00a0<code>'1'<\/code>s (also possibly 0.)<\/p>\n<p>We are given a string\u00a0<code>S<\/code>\u00a0of\u00a0<code>'0'<\/code>s and\u00a0<code>'1'<\/code>s, and we may flip any\u00a0<code>'0'<\/code>\u00a0to a\u00a0<code>'1'<\/code>\u00a0or a\u00a0<code>'1'<\/code>\u00a0to a\u00a0<code>'0'<\/code>.<\/p>\n<p>Return the minimum number of flips to make\u00a0<code>S<\/code>\u00a0monotone increasing.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-1-1\">\"00110\"<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">1<\/span>\r\n<strong>Explanation: <\/strong>We flip the last digit to get 00111.\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-2-1\">\"010110\"<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">2<\/span>\r\n<strong>Explanation: <\/strong>We flip to get 011111, or alternatively 000111.\r\n<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-3-1\">\"00011000\"<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-3\">2<\/span>\r\n<strong>Explanation: <\/strong>We flip to get 00000000.\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>1 &lt;= S.length &lt;= 20000<\/code><\/li>\n<li><code>S<\/code>\u00a0only consists of\u00a0<code>'0'<\/code>\u00a0and\u00a0<code>'1'<\/code>\u00a0characters.<\/li>\n<\/ol>\n<h1><strong>Solution: DP<\/strong><\/h1>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4217\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/10\/926-ep228.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/10\/926-ep228.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/10\/926-ep228-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/10\/926-ep228-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p>l[i] := number of flips to make S[0] ~ S[i] become all 0s.<\/p>\n<p>r[i] := number of flips to make S[i] ~ S[n &#8211; 1] become all 1s.<\/p>\n<p>Try all possible break point, S[0] ~ S[i &#8211; 1] are all 0s and S[i] ~ S[n-1] are all 1s.<\/p>\n<p>ans = min{l[i &#8211; 1] + r[i]}<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n  int minFlipsMonoIncr(string S) {\r\n    const int n = S.size();\r\n    vector&lt;int&gt; l(n + 1); \/\/ 1 -&gt; 0\r\n    vector&lt;int&gt; r(n + 1); \/\/ 0 -&gt; 1\r\n    l[0] = S[0] - '0';\r\n    r[n - 1] = '1' - S[n - 1];\r\n    for (int i = 1; i &lt; n; ++i)\r\n      l[i] = l[i - 1] + S[i] - '0';\r\n    for (int i = n - 2; i &gt;= 0; --i)\r\n      r[i] = r[i + 1] + '1' - S[i];\r\n    int ans = r[0]; \/\/ all 1s.\r\n    for (int i = 1; i &lt;= n; ++i)\r\n      ans = min(ans, l[i - 1] + r[i]);\r\n    return ans;\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">C++ v2<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n  int minFlipsMonoIncr(string S) {\r\n    const int n = S.length();\r\n    vector&lt;vector&lt;int&gt;&gt; dp(n + 1, vector&lt;int&gt;(2));\r\n    for (int i = 1; i &lt;= n; ++i) {\r\n      if (S[i - 1] == '0') {\r\n        dp[i][0] = dp[i - 1][0];\r\n        dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + 1;\r\n      } else {\r\n        dp[i][0] = dp[i - 1][0] + 1;\r\n        dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]);\r\n      }\r\n    }\r\n    return min(dp[n][0], dp[n][1]);\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">C++ v2 \/ O(1) Space<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n  int minFlipsMonoIncr(string S) {\r\n    const int n = S.length();\r\n    int dp0 = 0;\r\n    int dp1 = 0;\r\n    for (int i = 1; i &lt;= n; ++i) {\r\n      int tmp0 = dp0 + (S[i - 1] == '1');\r\n      dp1 = min(dp0, dp1) + (S[i - 1] == '0');\r\n      dp0 = tmp0;\r\n    }\r\n    return min(dp0, dp1);\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem A string of\u00a0&#8216;0&#8217;s and\u00a0&#8216;1&#8217;s is\u00a0monotone increasing\u00a0if it consists of some number of\u00a0&#8216;0&#8217;s (possibly 0), followed by some number of\u00a0&#8216;1&#8217;s (also possibly 0.) We are&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[18,302,425],"class_list":["post-4208","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-flip","tag-two-sides","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4208","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=4208"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4208\/revisions"}],"predecessor-version":[{"id":4218,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4208\/revisions\/4218"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}