Find minimum in rotated sorted Array
Question Solution
Suppose a sorted array is rotated at some unknown to youbeforehand.
(I. e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2 ).
Find the minimum element.
You may assume no duplicate exists in the array.
Solution 1:
The trick of this question is that the number of times it rotates is unknown. So it may have turned around and turned back to its original location.
Therefore, our code needs to handle two situations:
We still use the binary method. Compared to the full scan, the binary method can achieve logn complexity.
1. Sorted
This case is simple. First, calculate the value of MID. If it is in the middle of the left and right, it proves that this series is a normal sorted and returns directly to the left.
2. Center disconnection (that is, rotating too)
Our goal is to find out where there is a fracture. So we can calculate the value of mid every time, compare the mid to the left, if it is a normal order, we will lose the left, and vice versa, and continue to find the fracture again and again.
Analysis:
For example, after a sequence is disconnected from the center, it is composed of an ordered sequence and an unordered sequence.
If left = 0, Right = 6, mid = 3, then 4, 5, 6, 7 is positive, 7, 0, 1, 2 is backward, so we should discard the left. In this way, the operation is repeated until there are only two numbers in the series, that is, the disconnections. We will get 7, 0, and the last one will be returned.
It shows a simple description of how to gradually approach the fracture through three steps. Every time we get rid of half of it, the speed is logn.
Note that mid cannot be lost when half side is lost, because mid may be the answer.
Example: 3, 1, 2, 3, and 1 are in reverse order, and 1 and 2 are in positive order. If you throw 1 or 2, you lose the answer.
1 // solution 1: 2 Public int findmin1 (INT [] num) {3 if (num = NULL | num. length = 0) {4 return 0; 5} 6 7 if (Num. length = 1) {8 return num [0]; 9} 10 11 12 // There are at least two elements to discuss the value of 13 int L = 0; 14 int r = num. length-1; 15 16 while (L <r) {17 int mid = L + (R-l)/2; 18 // means that there is no rotate.19 if (Num [Mid]> = num [l] & num [Mid] <= num [R]) {20 return num [0]; 21} 22 23 // If rotate> 0, 24 if (L = r-1) {25 // when there are only two remaining elements, here is the breakpoint, and on the right is a small value 26 return num [R]; 27} 28 29 If (Num [Mid]> = num [l]) {30 // The left side is sorted. discard left.31 L = mid; 32} else {33 // The right side is sorted.34 r = mid; 35} 36} 37 38 return 0; 39}View code
Solution 2:
Using the binary template of the 9-Chapter algorithm: The biggest benefit of this template is that when the while (left <right-1) is like this, the termination must be left = right-1, in addition, the mid will always be in the middle and will not depend on left. It is quite convenient to judge.
1 // solution 2: 2 Public int findmin (INT [] A) {3 if (a = NULL |. length = 0) {4 return 0; 5} 6 7 if (. length = 1) {8 return a [0]; 9} else if (. length = 2) {10 return math. min (A [0], a [1]); 11} 12 13 // There are at least three elements to discuss the value of 14 int L = 0; 15 int r =. length-1; 16 17 while (L <r-1) {18 int M = L + (R-l)/2; 19 20 // means that there is no rotate.21 if (a [m] <A [R] & A [m]> A [l]) {22 return a [0]; 23} 24 25 // left side is sorted.26 if (a [m]> A [l]) {27 L = m; 28} else {29 R = m; 30} 31} 32 33 return a [R]; 34}View code. Therefore, you must use templated programming in the future, especially the binary template summarized here:
1 While (L <r-1) {2 int M = L + (R-l)/2; 3 4 // means that there is no rotate. 5... various exit conditions are added here. For example, 8 9 is found for the target value. // left side is sorted.10 if (a [m]> A [l]) {11 L = m; 12} else {13 r = m; 14} 15}
GitHub:
GitHub code Link
New Question of leetcode: Find minimum in rotated sorted array solution Report-solution to the binary Template