<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>two sides Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/two-sides/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/two-sides/</link>
	<description></description>
	<lastBuildDate>Wed, 24 Oct 2018 03:53:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>two sides Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/two-sides/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 926. Flip String to Monotone Increasing</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-926-flip-string-to-monotone-increasing/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-926-flip-string-to-monotone-increasing/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 21 Oct 2018 04:22:56 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[flip]]></category>
		<category><![CDATA[two sides]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4208</guid>

					<description><![CDATA[<p>Problem A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possibly 0), followed by some number of '1's (also possibly 0.) We are&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-926-flip-string-to-monotone-increasing/">花花酱 LeetCode 926. Flip String to Monotone Increasing</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><iframe width="500" height="375" src="https://www.youtube.com/embed/D8xa8ZMV7AI?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<h1><strong>Problem</strong></h1>
<p>A string of <code>'0'</code>s and <code>'1'</code>s is <em>monotone increasing</em> if it consists of some number of <code>'0'</code>s (possibly 0), followed by some number of <code>'1'</code>s (also possibly 0.)</p>
<p>We are given a string <code>S</code> of <code>'0'</code>s and <code>'1'</code>s, and we may flip any <code>'0'</code> to a <code>'1'</code> or a <code>'1'</code> to a <code>'0'</code>.</p>
<p>Return the minimum number of flips to make <code>S</code> monotone increasing.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-1-1">"00110"</span>
<strong>Output: </strong><span id="example-output-1">1</span>
<strong>Explanation: </strong>We flip the last digit to get 00111.
</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-2-1">"010110"</span>
<strong>Output: </strong><span id="example-output-2">2</span>
<strong>Explanation: </strong>We flip to get 011111, or alternatively 000111.
</pre>
<p><strong>Example 3:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-3-1">"00011000"</span>
<strong>Output: </strong><span id="example-output-3">2</span>
<strong>Explanation: </strong>We flip to get 00000000.
</pre>
<p><strong>Note:</strong></p>
<ol>
<li><code>1 &lt;= S.length &lt;= 20000</code></li>
<li><code>S</code> only consists of <code>'0'</code> and <code>'1'</code> characters.</li>
</ol>
<h1><strong>Solution: DP</strong></h1>
<p><img 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="(max-width: 960px) 100vw, 960px" /></p>
<p>l[i] := number of flips to make S[0] ~ S[i] become all 0s.</p>
<p>r[i] := number of flips to make S[i] ~ S[n &#8211; 1] become all 1s.</p>
<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>
<p>ans = min{l[i &#8211; 1] + r[i]}</p>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(n)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int minFlipsMonoIncr(string S) {
    const int n = S.size();
    vector&lt;int&gt; l(n + 1); // 1 -&gt; 0
    vector&lt;int&gt; r(n + 1); // 0 -&gt; 1
    l[0] = S[0] - '0';
    r[n - 1] = '1' - S[n - 1];
    for (int i = 1; i &lt; n; ++i)
      l[i] = l[i - 1] + S[i] - '0';
    for (int i = n - 2; i &gt;= 0; --i)
      r[i] = r[i + 1] + '1' - S[i];
    int ans = r[0]; // all 1s.
    for (int i = 1; i &lt;= n; ++i)
      ans = min(ans, l[i - 1] + r[i]);
    return ans;
  }
};</pre><p></div><h2 class="tabtitle">C++ v2</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int minFlipsMonoIncr(string S) {
    const int n = S.length();
    vector&lt;vector&lt;int&gt;&gt; dp(n + 1, vector&lt;int&gt;(2));
    for (int i = 1; i &lt;= n; ++i) {
      if (S[i - 1] == '0') {
        dp[i][0] = dp[i - 1][0];
        dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + 1;
      } else {
        dp[i][0] = dp[i - 1][0] + 1;
        dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]);
      }
    }
    return min(dp[n][0], dp[n][1]);
  }
};</pre><p></div><h2 class="tabtitle">C++ v2 / O(1) Space</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int minFlipsMonoIncr(string S) {
    const int n = S.length();
    int dp0 = 0;
    int dp1 = 0;
    for (int i = 1; i &lt;= n; ++i) {
      int tmp0 = dp0 + (S[i - 1] == '1');
      dp1 = min(dp0, dp1) + (S[i - 1] == '0');
      dp0 = tmp0;
    }
    return min(dp0, dp1);
  }
};</pre><p></div></div></p>
<p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-926-flip-string-to-monotone-increasing/">花花酱 LeetCode 926. Flip String to Monotone Increasing</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-926-flip-string-to-monotone-increasing/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
