<?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>Divide and conquer Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/category/divide-and-conquer/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/category/divide-and-conquer/</link>
	<description></description>
	<lastBuildDate>Sun, 01 Dec 2019 21:11:52 +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>Divide and conquer Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/category/divide-and-conquer/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1274. Number of Ships in a Rectangle</title>
		<link>https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-1274-number-of-ships-in-a-rectangle/</link>
					<comments>https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-1274-number-of-ships-in-a-rectangle/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 01 Dec 2019 21:06:00 +0000</pubDate>
				<category><![CDATA[Divide and conquer]]></category>
		<category><![CDATA[divide and conquer]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[interactive]]></category>
		<category><![CDATA[quad tree]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5900</guid>

					<description><![CDATA[<p>(This problem is an&#160;interactive problem.) On the sea represented by a cartesian plane, each ship is located at an integer point, and each integer point&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-1274-number-of-ships-in-a-rectangle/">花花酱 LeetCode 1274. Number of Ships in a Rectangle</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><em>(This problem is an&nbsp;<strong>interactive problem</strong>.)</em></p>



<p>On the sea represented by a cartesian plane, each ship is located at an integer point, and each integer point may contain at most 1 ship.</p>



<p>You have a function&nbsp;<code>Sea.hasShips(topRight, bottomLeft)</code>&nbsp;which takes two points&nbsp;as arguments and returns&nbsp;<code>true</code>&nbsp;if and only if there is at least one ship in the rectangle represented by the two points, including on the boundary.</p>



<p>Given two points, which are the top right and bottom left corners of a rectangle, return the number of ships present in that rectangle.&nbsp;&nbsp;It is guaranteed that there are&nbsp;<strong>at most 10 ships</strong>&nbsp;in that rectangle.</p>



<p>Submissions making&nbsp;<strong>more than 400 calls</strong>&nbsp;to&nbsp;<code>hasShips</code>&nbsp;will be judged&nbsp;<em>Wrong Answer</em>.&nbsp; Also, any solutions that attempt to circumvent the judge&nbsp;will result in disqualification.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2019/07/26/1445_example_1.PNG" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> 
ships = [[1,1],[2,2],[3,3],[5,5]], topRight = [4,4], bottomLeft = [0,0]
<strong>Output:</strong> 3
<strong>Explanation:</strong> From [0,0] to [4,4] we can count 3 ships within the range.
</pre>



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



<ul><li>On the input&nbsp;<code>ships</code>&nbsp;is only given to initialize the map internally.&nbsp;You must solve this problem &#8220;blindfolded&#8221;. In other words, you must find the answer using the given&nbsp;<code>hasShips</code>&nbsp;API, without knowing the&nbsp;<code>ships</code>&nbsp;position.</li><li><code>0 &lt;=&nbsp;bottomLeft[0]&nbsp;&lt;= topRight[0]&nbsp;&lt;= 1000</code></li><li><code>0 &lt;=&nbsp;bottomLeft[1]&nbsp;&lt;= topRight[1]&nbsp;&lt;= 1000</code></li></ul>



<h2><strong>Solution: Divide and Conquer</strong></h2>



<p>If the current rectangle contains ships, subdivide it into 4 smaller ones until <br>1) no ships contained<br>2) the current rectangle is a single point (e.g. topRight == bottomRight)</p>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  // Modify the interface to pass sea as a reference.
  int countShips(Sea&amp; sea, vector&lt;int&gt; topRight, vector&lt;int&gt; bottomLeft) {
    int x1 = bottomLeft[0], y1 = bottomLeft[1];
    int x2 = topRight[0], y2 = topRight[1];    
    if (x1 &gt; x2 || y1 &gt; y2 || !sea.hasShips(topRight, bottomLeft))
      return 0;
    if (x1 == x2 &amp;&amp; y1 == y2)
      return 1;
    int xm = x1 + (x2 - x1) / 2;
    int ym = y1 + (y2 - y1) / 2;
    return countShips(sea, {xm, ym}, {x1, y1}) + countShips(sea, {xm, y2}, {x1, ym + 1})
         + countShips(sea, {x2, ym}, {xm + 1, y1}) + countShips(sea, {x2, y2}, {xm + 1, ym + 1});
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-1274-number-of-ships-in-a-rectangle/">花花酱 LeetCode 1274. Number of Ships in a Rectangle</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-1274-number-of-ships-in-a-rectangle/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>
		<item>
		<title>花花酱  LeetCode 169. Majority Element</title>
		<link>https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-169-majority-element/</link>
					<comments>https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-169-majority-element/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 05 Nov 2017 06:55:23 +0000</pubDate>
				<category><![CDATA[Bit]]></category>
		<category><![CDATA[Divide and conquer]]></category>
		<category><![CDATA[Easy]]></category>
		<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[voting]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=738</guid>

					<description><![CDATA[<p>题目大意：给你一个数组，其中一个数出现超过n/2次，问你出现次数最多的那个数是什么？ Problem: Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-169-majority-element/">花花酱  LeetCode 169. Majority Element</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/LPIvL-jvGdA?feature=oembed" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen></iframe></p>
<p>题目大意：给你一个数组，其中一个数出现超过n/2次，问你出现次数最多的那个数是什么？</p>
<p><strong>Problem:</strong></p>
<p>Given an array of size <i>n</i>, find the majority element. The majority element is the element that appears <b>more than</b> <code>⌊ n/2 ⌋</code> times.</p>
<p>You may assume that the array is non-empty and the majority element always exist in the array.</p>
<p><strong>Ideas:</strong></p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/169-ep101.png"><img class="alignnone size-full wp-image-743" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/169-ep101.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/169-ep101.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/169-ep101-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/169-ep101-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/169-ep101-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script></p>
<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><br />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<p><strong>Solution 1:</strong></p>
<p>Hash table O(n) / O(n)</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime : 23 ms
class Solution {
public:
    int majorityElement(vector&lt;int&gt;&amp; nums) {
        unordered_map&lt;int, int&gt; count;
        const int n = nums.size();
        for (const int num : nums)
            if (++count[num] &gt; n / 2) return num;
        return -1;
    }
};</pre><p>&nbsp;</p>
<p><strong>Solution 2:</strong></p>
<p>BST O(nlogk) / O(n)</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime : 19 ms
class Solution {
public:
    int majorityElement(vector&lt;int&gt;&amp; nums) {
        map&lt;int, int&gt; count;
        const int n = nums.size();
        for (const int num : nums)
            if (++count[num] &gt; n / 2) return num;
        return -1;
    }
};</pre><p>&nbsp;</p>
<p><strong>Solution 3:</strong></p>
<p>Randomization O(n) / O(1)</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 19 ms
class Solution {
public:
    int majorityElement(vector&lt;int&gt;&amp; nums) {
        srand(time(nullptr));
        const int n = nums.size();
        while (true) {
            const int index = rand() % n;
            const int majority = nums[index];
            int count = 0;
            for (const int num : nums)
                if (num == majority &amp;&amp; ++count &gt; n/2) return num;
        }
        return -1;
    }
};</pre><p>&nbsp;</p>
<p><strong>Solution 4:</strong></p>
<p>Bit voting O(n) / O(1)</p><pre class="crayon-plain-tag">class Solution {
public:
    int majorityElement(vector&lt;int&gt;&amp; nums) {
        const int n = nums.size();
        int majority = 0;        
        for (int i = 0; i &lt; 32; ++i) {
            int mask = 1 &lt;&lt; i;
            int count = 0;
            for (const int num : nums)
                if ((num &amp; mask) &amp;&amp; (++count &gt; n /2)) {
                    majority |= mask;
                    break;
                }
        }
        return majority;
    }
};</pre><p>&nbsp;</p>
<p><strong>Solution 5:</strong></p>
<p>Moore Voting O(n) / O(1)</p><pre class="crayon-plain-tag">class Solution {
public:
    int majorityElement(vector&lt;int&gt;&amp; nums) {
        int majority = nums.front();
        int count = 0;
        
        for (const int num : nums) {
            if (num == majority) ++count;
            else if (--count == 0) {
                count = 1;
                majority = num;
            }
        }
        
        return majority;
    }
};</pre><p>&nbsp;</p>
<p>Solution 6:</p>
<p>Full sorting O(nlogn) / O(1)</p><pre class="crayon-plain-tag">class Solution {
public:
    int majorityElement(vector&lt;int&gt;&amp; nums) {
        std::sort(nums.begin(), nums.end());
        return nums[nums.size()/2];
    }
};</pre><p>&nbsp;</p>
<p>Solution 7:</p>
<p>Partial sorting O(n) / O(1)</p><pre class="crayon-plain-tag">class Solution {
public:
    int majorityElement(vector&lt;int&gt;&amp; nums) {
        nth_element(nums.begin(), nums.begin() + nums.size() / 2, nums.end());
        return nums[nums.size() / 2];
    }
};</pre><p>&nbsp;</p>
<p>Solution 8:</p>
<p>Divide and conquer O(nlogn) / O(logn)</p><pre class="crayon-plain-tag">class Solution {
public:
    int majorityElement(vector&lt;int&gt;&amp; nums) {
        return majorityElement(nums, 0, nums.size() - 1);
    }
private:
    int majorityElement(const vector&lt;int&gt;&amp; nums, int l, int r) {
        if (l == r) return nums[l];
        const int m = l + (r - l) / 2;
        const int ml = majorityElement(nums, l, m);
        const int mr = majorityElement(nums, m + 1, r);
        if (ml == mr) return ml;
        return count(nums.begin() + l, nums.begin() + r + 1, ml) &gt;
               count(nums.begin() + l, nums.begin() + r + 1, mr)
               ? ml : mr;
    }
};</pre><p>Divide and conquer O(nlogn) / O(logn)</p><pre class="crayon-plain-tag">class Solution {
public:
    int majorityElement(vector&lt;int&gt;&amp; nums) {
        return majorityElement(nums, 0, nums.size() - 1).first;
    }
private:
    pair&lt;int, int&gt; majorityElement(const vector&lt;int&gt;&amp; nums, int l, int r) {
        if (l == r) return {nums[l], 1};
        int mid = l + (r - l) / 2;
        auto ml = majorityElement(nums, l, mid);
        auto mr = majorityElement(nums, mid + 1, r);
        if (ml.first == mr.first) return { ml.first, ml.second + mr.second };
        if (ml.second &gt; mr.second)
            return { ml.first, ml.second + count(nums.begin() + mid + 1, nums.begin() + r + 1, ml.first) };
        else
            return { mr.first, mr.second + count(nums.begin() + l, nums.begin() + mid + 1, mr.first) };
    }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-169-majority-element/">花花酱  LeetCode 169. Majority Element</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-169-majority-element/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 719. Find K-th Smallest Pair Distance</title>
		<link>https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-719-find-k-th-smallest-pair-distance/</link>
					<comments>https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-719-find-k-th-smallest-pair-distance/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 29 Oct 2017 20:14:10 +0000</pubDate>
				<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[Divide and conquer]]></category>
		<category><![CDATA[Hard]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[bucket]]></category>
		<category><![CDATA[distance]]></category>
		<category><![CDATA[pair]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=710</guid>

					<description><![CDATA[<p>题目大意：给你一个数组，返回所有数对中，绝对值差第k小的值。 Problem: Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pair (A, B) is defined as the absolute&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-719-find-k-th-smallest-pair-distance/">花花酱 LeetCode 719. Find K-th Smallest Pair Distance</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/WHfljqX61Y8?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<p>题目大意：给你一个数组，返回所有数对中，绝对值差第k小的值。</p>
<p><strong>Problem:</strong></p>
<p>Given an integer array, return the k-th smallest <b>distance</b> among all the pairs. The distance of a pair (A, B) is defined as the absolute difference between A and B.</p>
<p><b>Example 1:</b></p><pre class="crayon-plain-tag">Input:
nums = [1,3,1]
k = 1
Output: 0 
Explanation:
Here are all the pairs:
(1,3) -&gt; 2
(1,1) -&gt; 0
(3,1) -&gt; 2
Then the 1st smallest distance pair is (1,1), and its distance is 0.</pre><p><b>Note:</b></p>
<ol>
<li><code>2 &lt;= len(nums) &lt;= 10000</code>.</li>
<li><code>0 &lt;= nums[i] &lt; 1000000</code>.</li>
<li><code>1 &lt;= k &lt;= len(nums) * (len(nums) - 1) / 2</code>.</li>
</ol>
<p><strong>Idea</strong></p>
<p>Bucket sort</p>
<p>Binary search / dp</p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/719-ep99-1.png"><img class="alignnone size-full wp-image-720" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/719-ep99-1.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/719-ep99-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/719-ep99-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/719-ep99-1-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/719-ep99-1-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/719-ep99-2.png"><img class="alignnone size-full wp-image-719" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/719-ep99-2.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/719-ep99-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/719-ep99-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/719-ep99-2-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/719-ep99-2-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p><strong>Solution</strong></p>
<p>C++ / binary search</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 9 ms
class Solution {
public:
    int smallestDistancePair(vector&lt;int&gt;&amp; nums, int k) {
        std::sort(nums.begin(), nums.end());
        int n = nums.size();
        int l = 0;
        int r = nums.back() - nums.front();
        while (l &lt;= r) {
            int cnt = 0;
            int j = 0;
            int m = l + (r - l) / 2;
            for (int i = 0; i &lt; n; ++i) {
                while (j &lt; n &amp;&amp; nums[j] - nums[i] &lt;= m) ++j;
                cnt += j - i - 1;
            }
            cnt &gt;= k ? r = m - 1 : l = m + 1;
        }        
        return l;
    }
};</pre><p>&nbsp;</p>
<p>C++ / bucket sort w/ vector O(n^2)</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 549 ms
class Solution {
public:
    int smallestDistancePair(vector&lt;int&gt;&amp; nums, int k) {
        std::sort(nums.begin(), nums.end());
        const int N = nums.back();
        vector&lt;int&gt; count(N + 1, 0);        
        const int n = nums.size();
        for (int i = 0; i &lt; n; ++i)
            for (int j = i + 1; j &lt; n; ++j)
               ++count[nums[j] - nums[i]];
        for (int i = 0; i &lt;= N; ++i) {
            k -= count[i];
            if (k &lt;= 0) return i;
        }
        return 0;
    }
};</pre><p><strong>Related Problems:</strong></p>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/two-pointers/leetcode-786-k-th-smallest-prime-fraction/">花花酱 LeetCode 786. K-th Smallest Prime Fraction</a></li>
<li><a href="http://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-378-kth-smallest-element-in-a-sorted-matrix/">花花酱 LeetCode 378. Kth Smallest Element in a Sorted Matrix</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-719-find-k-th-smallest-pair-distance/">花花酱 LeetCode 719. Find K-th Smallest Pair Distance</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-719-find-k-th-smallest-pair-distance/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 154. Find Minimum in Rotated Sorted Array II</title>
		<link>https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-154-find-minimum-in-rotated-sorted-array-ii/</link>
					<comments>https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-154-find-minimum-in-rotated-sorted-array-ii/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 08 Sep 2017 04:14:46 +0000</pubDate>
				<category><![CDATA[Divide and conquer]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[sorting]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=145</guid>

					<description><![CDATA[<p>Problem: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-154-find-minimum-in-rotated-sorted-array-ii/">花花酱 LeetCode 154. Find Minimum in Rotated Sorted Array II</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/aCb1zKMimDQ?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<p><strong>Problem:</strong></p>
<p>Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.</p>
<p>(i.e., <code>0 1 2 4 5 6 7</code> might become <code>4 5 6 7 0 1 2</code>).</p>
<p>Find the minimum element.</p>
<p>The array may contain duplicates.</p>
<p><strong>Idea: </strong></p>
<p>Divide and conquer</p>
<p><strong>Time complexity:</strong></p>
<p>Average: O(logn)</p>
<p>Worst: O(n)</p>
<p><strong>Solution:</strong></p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
    int findMin(vector&lt;int&gt; &amp;num) {
        return findMin(num, 0, num.size()-1);
    }
    
    int findMin(const vector&lt;int&gt;&amp; num, int l, int r)
    {
        // One or two elements, solve it directly
        if (l+1 &gt;= r) return
            min(num[l], num[r]);
        
        // Sorted
        if (num[l] &lt; num[r])
            return num[l];
        
        int m = l + (r-l)/2;
        
        // Recursively find the solution
        return min(findMin(num, l, m - 1), 
                   findMin(num, m, r));
    }
};</pre><p>&nbsp;</p>
<p><strong>Related Problems:</strong></p>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/leetcode/leetcode-153-find-minimum-in-rotated-sorted-array/">[解题报告] Leetcode 153. Find Minimum in Rotated Sorted Array</a></li>
<li><a href="http://zxi.mytechroad.com/blog/leetcode/leetcode-654-maximum-binary-tree/">[解题报告] LeetCode 654. Maximum Binary Tree</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-154-find-minimum-in-rotated-sorted-array-ii/">花花酱 LeetCode 154. Find Minimum in Rotated Sorted Array II</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-154-find-minimum-in-rotated-sorted-array-ii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 Leetcode 153. Find Minimum in Rotated Sorted Array</title>
		<link>https://zxi.mytechroad.com/blog/leetcode/leetcode-153-find-minimum-in-rotated-sorted-array/</link>
					<comments>https://zxi.mytechroad.com/blog/leetcode/leetcode-153-find-minimum-in-rotated-sorted-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 07 Sep 2017 05:42:12 +0000</pubDate>
				<category><![CDATA[Divide and conquer]]></category>
		<category><![CDATA[Leetcode]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=109</guid>

					<description><![CDATA[<p>Problem: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/leetcode/leetcode-153-find-minimum-in-rotated-sorted-array/">花花酱 Leetcode 153. Find Minimum in Rotated Sorted Array</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/P4r7mF1Jd50?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<p><strong>Problem</strong>:</p>
<p>Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.</p>
<p>(i.e., <code>0 1 2 4 5 6 7</code> might become <code>4 5 6 7 0 1 2</code>).</p>
<p>Find the minimum element.</p>
<p>You may assume no duplicate exists in the array.</p>
<p><strong>Idea</strong>:</p>
<p>Divide and conquer.</p>
<p>Evenly Split the array into two sub-arrays, and find the minimums of them, return the smaller one.</p>
<p>findMin(a[0..n]) = min(findMin(a[0..n/2], a[n/2..n])</p>
<p><strong>Key property</strong>:</p>
<p>One of the sub-array will be a sorted array, it takes O(1) to find the minimal element, just the first element.</p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/153-ep38-1.png"><img class="alignnone size-full wp-image-140" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/153-ep38-1.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/153-ep38-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/153-ep38-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/153-ep38-1-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/153-ep38-1-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p><img class="alignnone size-full wp-image-139" style="font-size: 1rem;" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/153-ep38-2.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/153-ep38-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/153-ep38-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/153-ep38-2-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/153-ep38-2-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<p><strong>Time complexity</strong>:</p>
<p>T(n) = O(1) + T(n/2) = O(logn)</p>
<p><strong>Solution:</strong></p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
    int findMin(vector&lt;int&gt; &amp;num) {
        return findMin(num, 0, num.size()-1);
    }
    
private:
    int findMin(const vector&lt;int&gt;&amp; num, int l, int r)
    {
        // Only 1 or 2 elements
        if (l+1 &gt;= r) return min(num[l], num[r]);
        
        // Sorted
        if (num[l] &lt; num[r]) return num[l];
        
        int mid = l + (r-l)/2; 
        
        return min(findMin(num, l, mid-1), 
                   findMin(num, mid, r));
    }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/leetcode/leetcode-153-find-minimum-in-rotated-sorted-array/">花花酱 Leetcode 153. Find Minimum in Rotated Sorted Array</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/leetcode/leetcode-153-find-minimum-in-rotated-sorted-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
