<?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>mergesort Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/mergesort/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/mergesort/</link>
	<description></description>
	<lastBuildDate>Mon, 30 Dec 2019 00:05:24 +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>mergesort Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/mergesort/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1305. All Elements in Two Binary Search Trees</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-1305-all-elements-in-two-binary-search-trees/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-1305-all-elements-in-two-binary-search-trees/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 29 Dec 2019 19:05:06 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[BST]]></category>
		<category><![CDATA[inorder]]></category>
		<category><![CDATA[mergesort]]></category>
		<category><![CDATA[O(n)]]></category>
		<category><![CDATA[traversal]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6021</guid>

					<description><![CDATA[<p>Given two binary search trees&#160;root1&#160;and&#160;root2. Return a list containing&#160;all the integers&#160;from&#160;both trees&#160;sorted in&#160;ascending&#160;order. Example 1: Input: root1 = [2,1,4], root2 = [1,0,3] Output: [0,1,1,2,3,4] Example&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-1305-all-elements-in-two-binary-search-trees/">花花酱 LeetCode 1305. All Elements in Two Binary Search Trees</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 1305. All Elements in Two Binary Search Trees - 刷题找工作 EP290" width="500" height="375" src="https://www.youtube.com/embed/2cbsWlAHlj4?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>Given two binary search trees&nbsp;<code>root1</code>&nbsp;and&nbsp;<code>root2</code>.</p>



<p>Return a list containing&nbsp;<em>all the integers</em>&nbsp;from&nbsp;<em>both trees</em>&nbsp;sorted in&nbsp;<strong>ascending</strong>&nbsp;order.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3]
<strong>Output:</strong> [0,1,1,2,3,4]
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> root1 = [0,-10,10], root2 = [5,1,7,0,2]
<strong>Output:</strong> [-10,0,0,1,2,5,7,10]
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> root1 = [], root2 = [5,1,7,0,2]
<strong>Output:</strong> [0,1,2,5,7]
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> root1 = [0,-10,10], root2 = []
<strong>Output:</strong> [-10,0,10]
</pre>



<p><strong>Example 5:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> root1 = [1,null,8], root2 = [8,1]
<strong>Output:</strong> [1,1,8,8]
</pre>



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



<ul><li>Each tree has at most&nbsp;<code>5000</code>&nbsp;nodes.</li><li>Each node&#8217;s value is between&nbsp;<code>[-10^5, 10^5]</code>.</li></ul>



<h2><strong>Solution: Inorder traversal + Merge Sort</strong></h2>



<p>Time complexity: O(t1 + t2)<br>Space complexity: O(t1 + t2)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; getAllElements(TreeNode* root1, TreeNode* root2) {    
    function&lt;void(TreeNode*, vector&lt;int&gt;&amp;)&gt; inorder = [&amp;](TreeNode* root, vector&lt;int&gt;&amp; t) {
      if (!root) return;
      inorder(root-&gt;left, t);
      t.push_back(root-&gt;val);
      inorder(root-&gt;right, t);
    };
    vector&lt;int&gt; t1;
    vector&lt;int&gt; t2;
    inorder(root1, t1);
    inorder(root2, t2);
    vector&lt;int&gt; m;
    int i = 0;
    int j = 0;
    while (m.size() != t1.size() + t2.size()) {
      if (j == t2.size()) m.push_back(t1[i++]);
      else if (i == t1.size()) m.push_back(t2[j++]);
      else m.push_back(t1[i] &lt; t2[j] ? t1[i++] : t2[j++]);      
    }
    return m;
  }
};</pre>
</div></div>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; getAllElements(TreeNode* root1, TreeNode* root2) {    
    function&lt;void(TreeNode*, vector&lt;int&gt;&amp;)&gt; inorder = [&amp;](TreeNode* root, vector&lt;int&gt;&amp; t) {
      if (!root) return;
      inorder(root-&gt;left, t);
      t.push_back(root-&gt;val);
      inorder(root-&gt;right, t);
    };
    vector&lt;int&gt; t1;
    vector&lt;int&gt; t2;
    inorder(root1, t1);
    inorder(root2, t2);
    vector&lt;int&gt; m;
    std::merge(begin(t1), end(t1), begin(t2), end(t2), back_inserter(m));    
    return m;
  }
};</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-1305-all-elements-in-two-binary-search-trees/">花花酱 LeetCode 1305. All Elements in Two Binary Search Trees</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/tree/leetcode-1305-all-elements-in-two-binary-search-trees/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 148. Sort List</title>
		<link>https://zxi.mytechroad.com/blog/list/leetcode-148-sort-list/</link>
					<comments>https://zxi.mytechroad.com/blog/list/leetcode-148-sort-list/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 27 Jul 2018 04:57:00 +0000</pubDate>
				<category><![CDATA[Divide and conquer]]></category>
		<category><![CDATA[List]]></category>
		<category><![CDATA[fast slow]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[mergesort]]></category>
		<category><![CDATA[sort]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=3320</guid>

					<description><![CDATA[<p>Problem Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4-&#62;2-&#62;1-&#62;3 Output: 1-&#62;2-&#62;3-&#62;4 Example 2: Input: -1-&#62;5-&#62;3-&#62;4-&#62;0 Output: -1-&#62;0-&#62;3-&#62;4-&#62;5 Solution: Merge&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-148-sort-list/">花花酱 LeetCode 148. Sort List</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/M1TwY0nsTZA?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<h1><strong>Problem</strong></h1>
<p>Sort a linked list in <em>O</em>(<em>n</em> log <em>n</em>) time using constant space complexity.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false "><strong>Input:</strong> 4-&gt;2-&gt;1-&gt;3
<strong>Output:</strong> 1-&gt;2-&gt;3-&gt;4
</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input:</strong> -1-&gt;5-&gt;3-&gt;4-&gt;0
<strong>Output:</strong> -1-&gt;0-&gt;3-&gt;4-&gt;5</pre>
<p><img class="alignnone size-full wp-image-3330" style="font-size: 16px;" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/148-ep211.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/148-ep211.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/148-ep211-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/148-ep211-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /><img class="alignnone size-full wp-image-3329" style="font-size: 16px;" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/148-ep211-2.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/148-ep211-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/148-ep211-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/148-ep211-2-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<h1>Solution: Merge Sort</h1>
<p>Top-down (recursion)</p>
<p>Time complexity: O(nlogn)</p>
<p>Space complexity: O(logn)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 32 ms
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
  ListNode* sortList(ListNode* head) {
    // 0 or 1 element, we are done.
    if (!head || !head-&gt;next) return head;
    ListNode* slow = head;
    ListNode* fast = head-&gt;next;    
    while (fast &amp;&amp; fast-&gt;next) {
      fast = fast-&gt;next-&gt;next;
      slow = slow-&gt;next;
    }
    ListNode* mid = slow-&gt;next;    
    slow-&gt;next = nullptr; // Break the list.
    return merge(sortList(head), sortList(mid));
  }
private:
  ListNode* merge(ListNode* l1, ListNode* l2) {
    ListNode dummy(0);
    ListNode* tail = &amp;dummy;
    while (l1 &amp;&amp; l2) {
      if (l1-&gt;val &gt; l2-&gt;val) swap(l1, l2);
      tail-&gt;next = l1;
      l1 = l1-&gt;next;
      tail = tail-&gt;next;
    }
    tail-&gt;next = l1 ? l1 : l2;    
    return dummy.next;
  }
};</pre><p></div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 6 ms
class Solution {
  public ListNode sortList(ListNode head) {
    if (head == null || head.next == null) return head;
    ListNode slow = head;
    ListNode fast = head.next;
    while (fast != null &amp;&amp; fast.next != null) {
      fast = fast.next.next;
      slow = slow.next;
    }
    ListNode mid = slow.next;
    slow.next = null;
    return merge(sortList(head), sortList(mid));
  }
  
  private ListNode merge(ListNode l1, ListNode l2) {
    ListNode dummy = new ListNode(0);
    ListNode tail = dummy;
    while (l1 != null &amp;&amp; l2 != null) {
      if (l1.val &gt; l2.val) {
        ListNode tmp = l1;
        l1 = l2;
        l2 = tmp;
      }
      tail.next = l1;
      l1 = l1.next;
      tail = tail.next;
    }
    tail.next = (l1 != null) ? l1 : l2;
    return dummy.next;
  }
}</pre><p></div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag"># Author: Huahua, 232 ms
class Solution:
  def sortList(self, head):
    def merge(l1, l2):
      dummy = ListNode(0)
      tail = dummy
      while l1 and l2:
        if l1.val &gt; l2.val: l1, l2 = l2, l1
        tail.next = l1
        l1 = l1.next
        tail = tail.next
      tail.next = l1 if l1 else l2
      return dummy.next
    
    if not head or not head.next: return head
    slow = head
    fast = head.next
    while fast and fast.next:
      fast = fast.next.next
      slow = slow.next
    mid = slow.next
    slow.next = None
    return merge(self.sortList(head), self.sortList(mid))</pre><p>&nbsp;</p>
<p></div></div></p>
<p>bottom up</p>
<p>Time complexity: O(nlogn)</p>
<p>Space complexity: O(1)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 32 ms
class Solution {
public:
  ListNode* sortList(ListNode* head) {
    // 0 or 1 element, we are done.
    if (!head || !head-&gt;next) return head;
    
    int len = 1;
    ListNode* cur = head;
    while (cur = cur-&gt;next) ++len;
    
    ListNode dummy(0);
    dummy.next = head;
    ListNode* l;
    ListNode* r;
    ListNode* tail;
    for (int n = 1; n &lt; len; n &lt;&lt;= 1) {      
      cur = dummy.next; // partial sorted head
      tail = &amp;dummy;
      while (cur) {
        l = cur;
        r = split(l, n);
        cur = split(r, n);
        auto merged = merge(l, r);
        tail-&gt;next = merged.first;
        tail = merged.second;
      }
    }      
    return dummy.next;
  }
private:
  // Splits the list into two parts, first n element and the rest.
  // Returns the head of the rest.
  ListNode* split(ListNode* head, int n) {    
    while (--n &amp;&amp; head)
      head = head-&gt;next;
    ListNode* rest = head ? head-&gt;next : nullptr;
    if (head) head-&gt;next = nullptr;
    return rest;
  }
  
  // Merges two lists, returns the head and tail of the merged list.
  pair&lt;ListNode*, ListNode*&gt; merge(ListNode* l1, ListNode* l2) {
    ListNode dummy(0);
    ListNode* tail = &amp;dummy;
    while (l1 &amp;&amp; l2) {
      if (l1-&gt;val &gt; l2-&gt;val) swap(l1, l2);
      tail-&gt;next = l1;
      l1 = l1-&gt;next;
      tail = tail-&gt;next;
    }
    tail-&gt;next = l1 ? l1 : l2;
    while (tail-&gt;next) tail = tail-&gt;next;
    return {dummy.next, tail};
  }
};</pre><p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-148-sort-list/">花花酱 LeetCode 148. Sort List</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/list/leetcode-148-sort-list/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>
