{"id":145,"date":"2017-09-07T21:14:46","date_gmt":"2017-09-08T04:14:46","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=145"},"modified":"2018-07-10T18:54:43","modified_gmt":"2018-07-11T01:54:43","slug":"leetcode-154-find-minimum-in-rotated-sorted-array-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/divide-and-conquer\/leetcode-154-find-minimum-in-rotated-sorted-array-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 154. Find Minimum in Rotated Sorted Array II"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"LeetCode 154. Find Minimum in Rotated Sorted Array II - \u82b1\u82b1\u9171 \u5237\u9898\u627e\u5de5\u4f5c EP39\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/aCb1zKMimDQ?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.<\/p>\n<p>(i.e.,\u00a0<code>0 1 2 4 5 6 7<\/code>\u00a0might become\u00a0<code>4 5 6 7 0 1 2<\/code>).<\/p>\n<p>Find the minimum element.<\/p>\n<p>The array may contain duplicates.<\/p>\n<p><strong>Idea:\u00a0<\/strong><\/p>\n<p>Divide and conquer<\/p>\n<p><strong>Time complexity:<\/strong><\/p>\n<p>Average: O(logn)<\/p>\n<p>Worst: O(n)<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n    int findMin(vector&lt;int&gt; &amp;num) {\r\n        return findMin(num, 0, num.size()-1);\r\n    }\r\n    \r\n    int findMin(const vector&lt;int&gt;&amp; num, int l, int r)\r\n    {\r\n        \/\/ One or two elements, solve it directly\r\n        if (l+1 &gt;= r) return\r\n            min(num[l], num[r]);\r\n        \r\n        \/\/ Sorted\r\n        if (num[l] &lt; num[r])\r\n            return num[l];\r\n        \r\n        int m = l + (r-l)\/2;\r\n        \r\n        \/\/ Recursively find the solution\r\n        return min(findMin(num, l, m - 1), \r\n                   findMin(num, m, r));\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Related Problems:<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/leetcode\/leetcode-153-find-minimum-in-rotated-sorted-array\/\">[\u89e3\u9898\u62a5\u544a] Leetcode 153. Find Minimum in Rotated Sorted Array<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/leetcode\/leetcode-654-maximum-binary-tree\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 654. Maximum Binary Tree<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e.,\u00a00 1 2 4 5 6 7\u00a0might become\u00a04&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[20,52,15],"class_list":["post-145","post","type-post","status-publish","format-standard","hentry","category-divide-and-conquer","tag-array","tag-binary-search","tag-sorting","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/145","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=145"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/145\/revisions"}],"predecessor-version":[{"id":3078,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/145\/revisions\/3078"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}