<?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>multistate Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/multistate/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/multistate/</link>
	<description></description>
	<lastBuildDate>Thu, 30 Aug 2018 21:22:11 +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>multistate Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/multistate/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 801. Minimum Swaps To Make Sequences Increasing</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-801-minimum-swaps-to-make-sequences-increasing/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-801-minimum-swaps-to-make-sequences-increasing/#comments</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 18 Mar 2018 05:56:46 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[multistate]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2188</guid>

					<description><![CDATA[<p>Problem 题目大意：给你两个数组A, B。问最少需要多少次对应位置元素的交换才能使得A和B变成单调递增。 https://leetcode.com/problems/minimum-swaps-to-make-sequences-increasing/description/ We have two integer sequences A and B of the same non-zero length. We are allowed to swap elements A[i] and B[i].  Note that both elements are in&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-801-minimum-swaps-to-make-sequences-increasing/">花花酱 LeetCode 801. Minimum Swaps To Make Sequences 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/__yxFFRQAl8?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<h1>Problem</h1>
<p>题目大意：给你两个数组A, B。问最少需要多少次对应位置元素的交换才能使得A和B变成单调递增。</p>
<p><a href="https://leetcode.com/problems/minimum-swaps-to-make-sequences-increasing/description/">https://leetcode.com/problems/minimum-swaps-to-make-sequences-increasing/description/</a></p>
<p>We have two integer sequences <code>A</code> and <code>B</code> of the same non-zero length.</p>
<p>We are allowed to swap elements <code>A[i]</code> and <code>B[i]</code>.  Note that both elements are in the same index position in their respective sequences.</p>
<p>At the end of some number of swaps, <code>A</code> and <code>B</code> are both strictly increasing.  (A sequence is <em>strictly increasing</em> if and only if <code>A[0] &lt; A[1] &lt; A[2] &lt; ... &lt; A[A.length - 1]</code>.)</p>
<p>Given A and B, return the minimum number of swaps to make both sequences strictly increasing.  It is guaranteed that the given input always makes it possible.</p>
<pre class="crayon:false "><strong>Example:</strong>
<strong>Input:</strong> A = [1,3,5,4], B = [1,2,3,7]
<strong>Output:</strong> 1
<strong>Explanation: </strong>
Swap A[3] and B[3].  Then the sequences are:
A = [1, 3, 5, 7] and B = [1, 2, 3, 4]
which are both strictly increasing.
</pre>
<p><strong>Note:</strong></p>
<ul>
<li><code>A, B</code> are arrays with the same length, and that length will be in the range <code>[1, 1000]</code>.</li>
<li><code>A[i], B[i]</code> are integer values in the range <code>[0, 2000]</code>.</li>
</ul>
<p><ins class="adsbygoogle" style="display: block; text-align: center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-2404451723245401" data-ad-slot="7983117522"> </ins></p>
<p><img class="alignnone size-full wp-image-2539" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/03/801-ep183.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/03/801-ep183.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/03/801-ep183-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/03/801-ep183-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<h1><strong>Solution: Search/DFS (TLE)</strong></h1>
<p>Time complexity: O(2^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
// Running time: TLE 84/102 test cases passed.
class Solution {
public:
  int minSwap(vector&lt;int&gt;&amp; A, vector&lt;int&gt;&amp; B) {
    int ans = INT_MAX;
    dfs(A, B, 0, 0, ans);
    return ans;
  }
private:
  void dfs(vector&lt;int&gt;&amp; A, vector&lt;int&gt;&amp; B, int i, int c, int&amp; ans) {
    if (c &gt;= ans) return;
    
    if (i == A.size()) {
      ans = min(ans, c);
      return;
    }
    
    if (i == 0 || A[i] &gt; A[i - 1] &amp;&amp; B[i] &gt; B[i - 1])
      dfs(A, B, i + 1, c, ans);
    
    if (i == 0 || A[i] &gt; B[i - 1] &amp;&amp; B[i] &gt; A[i - 1]) {
      swap(A[i], B[i]);
      dfs(A, B, i + 1, c + 1, ans);
      swap(A[i], B[i]);
    }
  }
};</pre><p></div></div><br />
<ins class="adsbygoogle" style="display: block; text-align: center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-2404451723245401" data-ad-slot="7983117522"> </ins></p>
<p>&nbsp;</p>
<h1><strong>Solution: DP</strong></h1>
<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
// Running time: 15 ms
class Solution {
public:
  int minSwap(vector&lt;int&gt;&amp; A, vector&lt;int&gt;&amp; B) {
    const int n = A.size();
        
    vector&lt;int&gt; keep(n, INT_MAX);
    vector&lt;int&gt; swap(n, INT_MAX);
    
    keep[0] = 0;
    swap[0] = 1;
    
    for (int i = 1; i &lt; n; ++i) {
      if (A[i] &gt; A[i - 1] &amp;&amp; B[i] &gt; B[i - 1]) {
        // Good case, no swapping needed.
        keep[i] = keep[i - 1];
    
        // Swapped A[i - 1] / B[i - 1], swap A[i], B[i] as well
        swap[i] = swap[i - 1] + 1;
      }      
      
      if (B[i] &gt; A[i - 1] &amp;&amp; A[i] &gt; B[i - 1]) {
        // A[i - 1] / B[i - 1] weren't swapped.
        swap[i] = min(swap[i], keep[i - 1] + 1);
      
        // Swapped A[i - 1] / B[i - 1], no swap needed for A[i] / B[i]      
        keep[i] = min(keep[i], swap[i - 1]);
      }      
    }
      
    return min(keep.back(), swap.back());
  }
};</pre><p></div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 60 ms
"""
class Solution:
  def minSwap(self, A, B):
    n = len(A)
    dp = [[float('inf'), float('inf')] for _ in range(n)]
    dp[0][0], dp[0][1] = 0, 1
    for i in range(1, n):
      if A[i] &gt; A[i - 1] and B[i] &gt; B[i - 1]:
        dp[i][0] = dp[i - 1][0]
        dp[i][1] = dp[i - 1][1] + 1
      if B[i] &gt; A[i - 1] and A[i] &gt; B[i - 1]:
        dp[i][0] = min(dp[i][0], dp[i - 1][1])
        dp[i][1] = min(dp[i][1], dp[i - 1][0] + 1)
    return min(dp[-1])</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-801-minimum-swaps-to-make-sequences-increasing/">花花酱 LeetCode 801. Minimum Swaps To Make Sequences 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-801-minimum-swaps-to-make-sequences-increasing/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
