{"id":109,"date":"2017-09-06T22:42:12","date_gmt":"2017-09-07T05:42:12","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=109"},"modified":"2018-07-10T18:56:02","modified_gmt":"2018-07-11T01:56:02","slug":"leetcode-153-find-minimum-in-rotated-sorted-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/leetcode\/leetcode-153-find-minimum-in-rotated-sorted-array\/","title":{"rendered":"\u82b1\u82b1\u9171 Leetcode 153. Find Minimum in Rotated Sorted Array"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"LeetCode 153. Find Minimum in Rotated Sorted Array - \u82b1\u82b1\u9171 \u5237\u9898\u627e\u5de5\u4f5c EP38\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/P4r7mF1Jd50?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>You may assume no duplicate exists in the array.<\/p>\n<p><strong>Idea<\/strong>:<\/p>\n<p>Divide and conquer.<\/p>\n<p>Evenly Split the array into two sub-arrays, and find the minimums of them, return the smaller one.<\/p>\n<p>findMin(a[0..n]) = min(findMin(a[0..n\/2], a[n\/2..n])<\/p>\n<p><strong>Key property<\/strong>:<\/p>\n<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>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/153-ep38-1.png\"><img loading=\"lazy\" decoding=\"async\" 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=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" 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=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p><strong>Time complexity<\/strong>:<\/p>\n<p>T(n) = O(1) + T(n\/2) = O(logn)<\/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\nprivate:\r\n    int findMin(const vector&lt;int&gt;&amp; num, int l, int r)\r\n    {\r\n        \/\/ Only 1 or 2 elements\r\n        if (l+1 &gt;= r) return min(num[l], num[r]);\r\n        \r\n        \/\/ Sorted\r\n        if (num[l] &lt; num[r]) return num[l];\r\n        \r\n        int mid = l + (r-l)\/2; \r\n        \r\n        return min(findMin(num, l, mid-1), \r\n                   findMin(num, mid, r));\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\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,3],"tags":[],"class_list":["post-109","post","type-post","status-publish","format-standard","hentry","category-divide-and-conquer","category-leetcode","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/109","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=109"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/109\/revisions"}],"predecessor-version":[{"id":3080,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/109\/revisions\/3080"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}