<?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>inversion Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/inversion/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/inversion/</link>
	<description></description>
	<lastBuildDate>Mon, 27 Aug 2018 15:11:28 +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>inversion Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/inversion/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 870. Advantage Shuffle</title>
		<link>https://zxi.mytechroad.com/blog/greedy/leetcode-870-advantage-shuffle/</link>
					<comments>https://zxi.mytechroad.com/blog/greedy/leetcode-870-advantage-shuffle/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 15 Jul 2018 05:43:37 +0000</pubDate>
				<category><![CDATA[Greedy]]></category>
		<category><![CDATA[inversion]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[multiset]]></category>
		<category><![CDATA[sorting]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=3168</guid>

					<description><![CDATA[<p>Problem Given two arrays A and B of equal size, the advantage of A with respect to B is the number of indices i for which A[i] &#62; B[i]. Return any permutation of A that maximizes its advantage with respect to B.&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-870-advantage-shuffle/">花花酱 LeetCode 870. Advantage Shuffle</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>
<p>Given two arrays <code>A</code> and <code>B</code> of equal size, the <em>advantage of <code>A</code> with respect to <code>B</code></em> is the number of indices <code>i</code> for which <code>A[i] &gt; B[i]</code>.</p>
<p>Return <strong>any</strong> permutation of <code>A</code> that maximizes its advantage with respect to <code>B</code>.</p>
<div>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>A = <span id="example-input-1-1">[2,7,11,15]</span>, B = <span id="example-input-1-2">[1,10,4,11]</span>
<strong>Output: </strong><span id="example-output-1">[2,11,7,15]</span>
</pre>
<div>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>A = <span id="example-input-2-1">[12,24,8,32]</span>, B = <span id="example-input-2-2">[13,25,32,11]</span>
<strong>Output: </strong><span id="example-output-2">[24,32,8,12]</span>
</pre>
<p><strong>Note:</strong></p>
<ol>
<li><code>1 &lt;= A.length = B.length &lt;= 10000</code></li>
<li><code>0 &lt;= A[i] &lt;= 10^9</code></li>
<li><code>0 &lt;= B[i] &lt;= 10^9</code></li>
</ol>
<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>
<h1><strong>Solution: Greedy 田忌赛马</strong></h1>
<p>Use the smallest unused number A[j] in A such that A[j] &gt; B[i], if not possible, use the smallest number in A.</p>
<p>Time complexity: O(nlogn)</p>
<p>Space complexity: O(n)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 124 ms
class Solution {
public:
  vector&lt;int&gt; advantageCount(vector&lt;int&gt;&amp; A, vector&lt;int&gt;&amp; B) {
    multiset&lt;int&gt; s(begin(A), end(A));
    vector&lt;int&gt; ans;    
    for (int b : B) {
      auto it = s.upper_bound(b);
      if (it == s.end()) it = s.begin();      
      ans.push_back(*it);
      s.erase(it);
    }
    return ans;
  }
};</pre><p>&nbsp;</p>
</div>
</div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-870-advantage-shuffle/">花花酱 LeetCode 870. Advantage Shuffle</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/greedy/leetcode-870-advantage-shuffle/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 775. Global and Local Inversions</title>
		<link>https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-775-global-and-local-inversions/</link>
					<comments>https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-775-global-and-local-inversions/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 09 Jul 2018 15:31:50 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[Divide and conquer]]></category>
		<category><![CDATA[divide and conquer]]></category>
		<category><![CDATA[inversion]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[mergesort]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=3044</guid>

					<description><![CDATA[<p>Problem We have some permutation A of [0, 1, ..., N - 1], where N is the length of A. The number of (global) inversions is the number of i &#60; j with 0&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-775-global-and-local-inversions/">花花酱 LeetCode 775. Global and Local Inversions</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/3QHSJSFm0W0?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<h1>Problem</h1>
<p>We have some permutation <code>A</code> of <code>[0, 1, ..., N - 1]</code>, where <code>N</code> is the length of <code>A</code>.</p>
<p>The number of (global) inversions is the number of <code>i &lt; j</code> with <code>0 &lt;= i &lt; j &lt; N</code> and <code>A[i] &gt; A[j]</code>.</p>
<p>The number of local inversions is the number of <code>i</code> with <code>0 &lt;= i &lt; N</code> and <code>A[i] &gt; A[i+1]</code>.</p>
<p>Return <code>true</code> if and only if the number of global inversions is equal to the number of local inversions.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input:</strong> A = [1,0,2]
<strong>Output:</strong> true
<strong>Explanation:</strong> There is 1 global inversion, and 1 local inversion.
</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false "><strong>Input:</strong> A = [1,2,0]
<strong>Output:</strong> false
<strong>Explanation:</strong> There are 2 global inversions, and 1 local inversion.
</pre>
<p><strong>Note:</strong></p>
<ul>
<li><code>A</code> will be a permutation of <code>[0, 1, ..., A.length - 1]</code>.</li>
<li><code>A</code> will have length in range <code>[1, 5000]</code>.</li>
<li>The time limit for this problem has been reduced.</li>
</ul>
<h1>Solution1: Brute Force (TLE)</h1>
<p>Time complexity: O(n^2)</p>
<p>Space complexity: O(1)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: TLE (208/208 test cases passed)
class Solution {
public:
  bool isIdealPermutation(vector&lt;int&gt;&amp; A) {
    const int n = A.size();
    int local = 0;
    for (int i = 0; i &lt; n - 1; ++i)
      if (A[i] &gt; A[i + 1]) ++local;
    int global = 0;
    for (int i = 0; i &lt; n; ++i)
      for (int j = i + 1; j &lt; n; ++j)
        if (A[i] &gt; A[j] &amp;&amp; ++global &gt; local) return false;
    return global == local;
  }
};</pre><p>&nbsp;</p>
<h1>Solution2: MergeSort</h1>
<p>Time complexity: O(nlogn)</p>
<p>Space complexity: O(n)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 52 ms (&lt;96.42%)
class Solution {
public:
  bool isIdealPermutation(vector&lt;int&gt;&amp; A) {
    const int n = A.size();
    int local = 0;
    for (int i = 0; i &lt; n - 1; ++i)
      if (A[i] &gt; A[i + 1]) ++local;
    tmp = vector&lt;int&gt;(n);
    int global = mergeSort(A, 0, n - 1);
    return global == local;
  }
private:
  vector&lt;int&gt; tmp;
  int mergeSort(vector&lt;int&gt;&amp; A, int l, int r) {
    if (l &gt;= r) return 0;
    const int len = r - l + 1;
    int m = (r - l) / 2 + l;
    int inv = mergeSort(A, l, m) + mergeSort(A, m + 1, r);
        
    int i = l;
    int j = m + 1;
    int k = 0;
    
    while (i &lt;= m &amp;&amp; j &lt;= r) {
      if (A[i] &lt;= A[j]) {
        tmp[k++] = A[i++];
      } else {
        tmp[k++] = A[j++];
        inv += m - i + 1;
      }
    }
    
    while (i &lt;= m) tmp[k++] = A[i++];
    while (j &lt;= r) tmp[k++] = A[j++];
    
    std::copy(tmp.begin(), tmp.begin() + len, A.begin() + l);
    
    return inv;
  }
};</pre><p>C#</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 208 ms
public class Solution {
  public bool IsIdealPermutation(int[] A) {
    var n = A.Length;
    var localInv = 0;
    for (var i = 1; i &lt; n; ++i)
      if (A[i] &lt; A[i - 1]) ++localInv;
    tmp = new int[n];
    var globalInv = MergeSort(A, 0, n - 1);    
    return localInv == globalInv;
  }  
  private int[] tmp;  
  private int MergeSort(int[] A, int l, int r) {
    if (l &gt;= r) return 0;
    int m = (r - l) / 2 + l;
    int inv = MergeSort(A, l, m) + MergeSort(A, m + 1, r);
    int i = l;
    int j = m + 1;
    int k = 0;
    
    while (i &lt;= m &amp;&amp; j &lt;= r) {
      if (A[i] &lt;= A[j]) {
        tmp[k++] = A[i++];
      } else {
        inv += m - i + 1;
        tmp[k++] = A[j++];
      }
    }
    
    while (i &lt;= m) tmp[k++] = A[i++];
    while (j &lt;= r) tmp[k++] = A[j++];
    
    Array.Copy(tmp, 0, A, l, k);
    
    return inv;
  }
}</pre><p>&nbsp;</p>
<h1>Solution3: Input Property</h1>
<p>Input is a permutation of [0, 1, &#8230;, N &#8211; 1]</p>
<p>Time Complexity: O(n)</p>
<p>Space Complexity: O(1)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 40 ms (&lt;99.26%)
class Solution {
public:
  bool isIdealPermutation(vector&lt;int&gt;&amp; A) {
    for (int i = 0; i &lt; A.size(); ++i)
        if (abs(A[i] - i) &gt; 1) return false;
	  return true;
  }
};</pre><p>&nbsp;</p>
<p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-775-global-and-local-inversions/">花花酱 LeetCode 775. Global and Local Inversions</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/divide-and-conquer/leetcode-775-global-and-local-inversions/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
