<?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>flip Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/flip/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/flip/</link>
	<description></description>
	<lastBuildDate>Fri, 31 Dec 2021 23:08:58 +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>flip Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/flip/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1975. Maximum Matrix Sum</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-1975-maximum-matrix-sum/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-1975-maximum-matrix-sum/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 31 Dec 2021 23:04:43 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[flip]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[matrix]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[negatives]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9350</guid>

					<description><![CDATA[<p>You are given an&#160;n x n&#160;integer&#160;matrix. You can do the following operation&#160;any&#160;number of times: Choose any two&#160;adjacent&#160;elements of&#160;matrix&#160;and&#160;multiply&#160;each of them by&#160;-1. Two elements are considered&#160;adjacent&#160;if&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-1975-maximum-matrix-sum/">花花酱 LeetCode 1975. Maximum Matrix Sum</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>You are given an&nbsp;<code>n x n</code>&nbsp;integer&nbsp;<code>matrix</code>. You can do the following operation&nbsp;<strong>any</strong>&nbsp;number of times:</p>



<ul><li>Choose any two&nbsp;<strong>adjacent</strong>&nbsp;elements of&nbsp;<code>matrix</code>&nbsp;and&nbsp;<strong>multiply</strong>&nbsp;each of them by&nbsp;<code>-1</code>.</li></ul>



<p>Two elements are considered&nbsp;<strong>adjacent</strong>&nbsp;if and only if they share a&nbsp;<strong>border</strong>.</p>



<p>Your goal is to&nbsp;<strong>maximize</strong>&nbsp;the summation of the matrix&#8217;s elements. Return&nbsp;<em>the&nbsp;<strong>maximum</strong>&nbsp;sum of the matrix&#8217;s elements using the operation mentioned above.</em></p>



<p><strong>Example 1:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/07/16/pc79-q2ex1.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> matrix = [[1,-1],[-1,1]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> We can follow the following steps to reach sum equals 4:
- Multiply the 2 elements in the first row by -1.
- Multiply the 2 elements in the first column by -1.
</pre>



<p><strong>Example 2:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/07/16/pc79-q2ex2.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> matrix = [[1,2,3],[-1,-2,-3],[1,2,3]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> We can follow the following step to reach sum equals 16:
- Multiply the 2 last elements in the second row by -1.
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>n == matrix.length == matrix[i].length</code></li><li><code>2 &lt;= n &lt;= 250</code></li><li><code>-10<sup>5</sup>&nbsp;&lt;= matrix[i][j] &lt;= 10<sup>5</sup></code></li></ul>



<h2><strong>Solution: Math</strong></h2>



<p>Count the number of negative numbers. <br>1. Even negatives, we can always flip all the negatives to positives. ans = sum(abs(matrix)). <br>2. Odd negatives, there will be one negative left, we found the smallest abs(element) and let it become negative. ans = sum(abs(matrix))) &#8211; 2 * min(abs(matrix))</p>



<p>Time complexity: O(n<sup>2</sup>)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  long long maxMatrixSum(vector&lt;vector&lt;int&gt;&gt;&amp; matrix) {
    const int n = matrix.size();
    long long ans = 0;
    int count = 0;
    int lo = INT_MAX;
    for (int i = 0; i &lt; n; ++i)
      for (int j = 0; j &lt; n; ++j) {
        ans += abs(matrix[i][j]);
        lo = min(lo, abs(matrix[i][j]));
        count += matrix[i][j] &lt; 0;          
      }    
    return ans - (count &amp; 1) * 2 * lo;
  }
};</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-1975-maximum-matrix-sum/">花花酱 LeetCode 1975. Maximum Matrix Sum</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/math/leetcode-1975-maximum-matrix-sum/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1529. Bulb Switcher IV</title>
		<link>https://zxi.mytechroad.com/blog/bit/leetcode-1529-bulb-switcher-iv/</link>
					<comments>https://zxi.mytechroad.com/blog/bit/leetcode-1529-bulb-switcher-iv/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 26 Jul 2020 18:52:08 +0000</pubDate>
				<category><![CDATA[Bit]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[flip]]></category>
		<category><![CDATA[xor]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7166</guid>

					<description><![CDATA[<p>There is a room with&#160;n&#160;bulbs, numbered from&#160;0&#160;to&#160;n-1,&#160;arranged in a row from left to right. Initially all the bulbs are&#160;turned off. Your task is to obtain&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-1529-bulb-switcher-iv/">花花酱 LeetCode 1529. Bulb Switcher IV</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>There is a room with&nbsp;<code>n</code>&nbsp;bulbs, numbered from&nbsp;<code>0</code>&nbsp;to&nbsp;<code>n-1</code>,&nbsp;arranged in a row from left to right. Initially all the bulbs are&nbsp;<strong>turned off</strong>.</p>



<p>Your task is to obtain the configuration represented by&nbsp;<code>target</code>&nbsp;where&nbsp;<code>target[i]</code>&nbsp;is &#8216;1&#8217; if the i-th bulb is turned on and is &#8216;0&#8217; if it is turned off.</p>



<p>You have a switch&nbsp;to flip the state of the bulb,&nbsp;a flip operation is defined as follows:</p>



<ul><li>Choose&nbsp;<strong>any</strong>&nbsp;bulb (index&nbsp;<code>i</code>)&nbsp;of your current configuration.</li><li>Flip each bulb from index&nbsp;<code>i</code>&nbsp;to&nbsp;<code>n-1</code>.</li></ul>



<p>When any bulb is flipped it means that if it is 0 it changes to 1 and if it is 1 it changes to 0.</p>



<p>Return the&nbsp;<strong>minimum</strong>&nbsp;number of flips required to form&nbsp;<code>target</code>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> target = "10111"
<strong>Output:</strong> 3
<strong>Explanation: </strong>Initial configuration "00000".
flip from the third bulb:  "00000" -&gt; "00111"
flip from the first bulb:  "00111" -&gt; "11000"
flip from the second bulb:  "11000" -&gt; "10111"
We need at least 3 flip operations to form target.</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> target = "101"
<strong>Output:</strong> 3
<strong>Explanation: </strong>"000" -&gt; "111" -&gt; "100" -&gt; "101".
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> target = "00000"
<strong>Output:</strong> 0
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> target = "001011101"
<strong>Output:</strong> 5
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= target.length &lt;= 10^5</code></li><li><code>target[i] == '0'</code>&nbsp;or&nbsp;<code>target[i] == '1'</code></li></ul>



<h2><strong>Solution: XOR</strong></h2>



<p>Flip from left to right, since flipping the a bulb won&#8217;t affect anything in the left.<br>We count how many times flipped so far, and that % 2 will be the state for all the bulb to the right.<br>If the current bulb&#8217;s state != target, we have to flip once.</p>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution {
public:
  int minFlips(string target) {
    int ans = 0;
    int cur = 0;
    for (char c : target) {
      if (c - '0' != cur) {
        cur ^= 1;
        ++ans;
      }
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-1529-bulb-switcher-iv/">花花酱 LeetCode 1529. Bulb Switcher IV</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/bit/leetcode-1529-bulb-switcher-iv/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 48. Rotate Image</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-48-rotate-image/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-48-rotate-image/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 02 Oct 2019 16:20:55 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[flip]]></category>
		<category><![CDATA[in place]]></category>
		<category><![CDATA[matrix]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[mirror]]></category>
		<category><![CDATA[O(n^2)]]></category>
		<category><![CDATA[rotate]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5698</guid>

					<description><![CDATA[<p>You are given an&#160;n&#160;x&#160;n&#160;2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Note: You have to rotate the image&#160;in-place, which means you&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-48-rotate-image/">花花酱 LeetCode 48. Rotate Image</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>You are given an&nbsp;<em>n</em>&nbsp;x&nbsp;<em>n</em>&nbsp;2D matrix representing an image.</p>



<p>Rotate the image by 90 degrees (clockwise).</p>



<p><strong>Note:</strong></p>



<p>You have to rotate the image&nbsp;<a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank" rel="noreferrer noopener"><strong>in-place</strong></a>, which means you have to modify the input 2D matrix directly.&nbsp;<strong>DO NOT</strong>&nbsp;allocate another 2D matrix and do the rotation.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false">Given <strong>input matrix</strong> = 
[
  [1,2,3],
  [4,5,6],
  [7,8,9]
],

rotate the input matrix <strong>in-place</strong> such that it becomes:
[
  [7,4,1],
  [8,5,2],
  [9,6,3]
]
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false">Given <strong>input matrix</strong> =
[
  [ 5, 1, 9,11],
  [ 2, 4, 8,10],
  [13, 3, 6, 7],
  [15,14,12,16]
], 

rotate the input matrix <strong>in-place</strong> such that it becomes:
[
  [15,13, 2, 5],
  [14, 3, 4, 1],
  [12, 6, 8, 9],
  [16, 7,10,11]
]</pre>



<h2><strong>Solution: 2 Passes</strong></h2>



<p>First pass: mirror around diagonal <br>Second pass: mirror around y axis</p>



<p>Time complexity: O(n^2)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  void rotate(vector&lt;vector&lt;int&gt;&gt;&amp; matrix) {
    const int n = matrix.size();
    for (int i = 0; i &lt; n; ++i)
      for (int j = i + 1; j &lt; n; ++j)
        swap(matrix[i][j], matrix[j][i]);
    for (int i = 0; i &lt; n; ++i)
      reverse(begin(matrix[i]), end(matrix[i]));
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-48-rotate-image/">花花酱 LeetCode 48. Rotate Image</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/algorithms/array/leetcode-48-rotate-image/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 978. Longest Turbulent Subarray</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-978-longest-turbulent-subarray/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-978-longest-turbulent-subarray/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 20 Jan 2019 08:42:50 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[flip]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4668</guid>

					<description><![CDATA[<p>A subarray&#160;A[i], A[i+1], ..., A[j]&#160;of&#160;A&#160;is said to be&#160;turbulent&#160;if and only if: For&#160;i &#60;= k &#60; j,&#160;A[k] &#62; A[k+1]&#160;when&#160;k&#160;is odd, and&#160;A[k] &#60; A[k+1]&#160;when&#160;k&#160;is even; OR, for&#160;i&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-978-longest-turbulent-subarray/">花花酱 LeetCode 978. Longest Turbulent Subarray</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>A subarray&nbsp;<code>A[i], A[i+1], ..., A[j]</code>&nbsp;of&nbsp;<code>A</code>&nbsp;is said to be&nbsp;<em>turbulent</em>&nbsp;if and only if:</p>



<ul><li>For&nbsp;<code>i &lt;= k &lt; j</code>,&nbsp;<code>A[k] &gt; A[k+1]</code>&nbsp;when&nbsp;<code>k</code>&nbsp;is odd, and&nbsp;<code>A[k] &lt; A[k+1]</code>&nbsp;when&nbsp;<code>k</code>&nbsp;is even;</li><li><strong>OR</strong>, for&nbsp;<code>i &lt;= k &lt; j</code>,&nbsp;<code>A[k] &gt; A[k+1]</code>&nbsp;when&nbsp;<code>k</code>&nbsp;is even, and&nbsp;<code>A[k] &lt; A[k+1]</code>when&nbsp;<code>k</code>&nbsp;is odd.</li></ul>



<p>That is, the subarray is turbulent if the comparison sign flips between each adjacent pair of elements in the subarray.</p>



<p>Return the&nbsp;<strong>length</strong>&nbsp;of a&nbsp;maximum size turbulent subarray of A.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>[9,4,2,10,7,8,8,1,9]
<strong>Output: </strong>5
<strong>Explanation: </strong>(A[1] &gt; A[2] &lt; A[3] &gt; A[4] &lt; A[5])
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>[4,8,12,16]
<strong>Output: </strong>2
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>[100]
<strong>Output: </strong>1
</pre>



<p><strong>Note:</strong></p>



<ol><li><code>1 &lt;= A.length &lt;= 40000</code></li><li><code>0 &lt;= A[i] &lt;= 10^9</code></li></ol>



<h2>Solution: DP</h2>



<p>Time complexity: O(n)<br>Space complexity: O(n) -> O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua, running time: 120 ms
class Solution {
public:
  int maxTurbulenceSize(vector&lt;int&gt;&amp; A) {
    vector&lt;int&gt; up(A.size(), 1);
    vector&lt;int&gt; down(A.size(), 1);
    int ans = 1;
    for (int i = 1; i &lt; A.size(); ++i) {
      if (A[i] &gt; A[i - 1]) up[i] = down[i - 1] + 1;
      if (A[i] &lt; A[i - 1]) down[i] = up[i - 1] + 1;
      ans = max(ans, max(up[i], down[i]));      
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-978-longest-turbulent-subarray/">花花酱 LeetCode 978. Longest Turbulent Subarray</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-978-longest-turbulent-subarray/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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>
		<item>
		<title>花花酱 LeetCode 832. Flipping an Image</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-832-flipping-an-image/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-832-flipping-an-image/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 13 May 2018 17:20:24 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[flip]]></category>
		<category><![CDATA[image]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2829</guid>

					<description><![CDATA[<p>Problem Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image. To flip an image horizontally&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-832-flipping-an-image/">花花酱 LeetCode 832. Flipping an Image</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<div class="question-description__2cX5">
<div>
<p>Given a binary matrix <code>A</code>, we want to flip the image horizontally, then invert it, and return the resulting image.</p>
<p>To flip an image horizontally means that each row of the image is reversed.  For example, flipping <code>[1, 1, 0]</code> horizontally results in <code>[0, 1, 1]</code>.</p>
<p>To invert an image means that each <code>0</code> is replaced by <code>1</code>, and each <code>1</code> is replaced by <code>0</code>. For example, inverting <code>[0, 1, 1]</code> results in <code>[1, 0, 0]</code>.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>[[1,1,0],[1,0,1],[0,0,0]]
<strong>Output: </strong>[[1,0,0],[0,1,0],[1,1,1]]
<strong>Explanation:</strong> First reverse each row: [[0,1,1],[1,0,1],[0,0,0]].
Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]]
</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false "><strong>Input: </strong>[[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
<strong>Output: </strong>[[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
<strong>Explanation:</strong> First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]].
Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
</pre>
<p><strong>Notes:</strong></p>
<ul>
<li><code>1 &lt;= A.length = A[0].length &lt;= 20</code></li>
<li><code>0 &lt;= A[i][j]<span style="font-family: sans-serif, Arial, Verdana, 'Trebuchet MS';"> &lt;= </span>1</code></li>
</ul>
</div>
</div>
<h1>Solution 1: Brute Force</h1>
<p>Time complexity: O(m*n)</p>
<p>Space complexity: O(m*n)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 15 ms
class Solution {
public:
  vector&lt;vector&lt;int&gt;&gt; flipAndInvertImage(vector&lt;vector&lt;int&gt;&gt;&amp; A) {
    auto B = A;
    int m = A.size();
    int n = A[0].size();
    for (int i = 0; i &lt; m; ++i)
      for (int j = 0; j &lt; n; ++j)
        B[i][j] = 1 - A[i][n - j - 1];
    return B;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-832-flipping-an-image/">花花酱 LeetCode 832. Flipping an Image</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/algorithms/array/leetcode-832-flipping-an-image/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
