Let's make this one less mysterious. For 153. Find Minimum in Rotated Sorted Array, the solution in this repository is mainly a binary search solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: binary search, two pointers, sliding window.
The notes already sitting in the source point us in the right direction:
- Binary search
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are findMin.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(logN)
- Space: O(1)
Guide
C++ Solution
Your submission
The accepted solution
01//Binary search
02//Runtime: 8 ms, faster than 61.21% of C++ online submissions for Find Minimum in Rotated Sorted Array.
03//Memory Usage: 10.1 MB, less than 84.69% of C++ online submissions for Find Minimum in Rotated Sorted Array.
04class Solution {
05public:
06 int findMin(vector<int>& nums) {
07 int n = nums.size();
08
09 int mid;
10 /*
11 choose nums[0] as pivot,
12 so we don't need to put nums[0] in the search range
13 */
14 int ans = nums[0];
15 int l = 1, r = n-1;
16
17 while(l <= r){
18 mid = (l+r) >> 1;
19 // cout << mid << endl;
20
21 if(nums[mid] < nums[0]){
22 ans = min(ans, nums[mid]);
23 r = mid-1;
24 }else{
25 //nums[mid] > nums[0]
26 l = mid+1;
27 }
28 }
29
30 return ans;
31 }
32};
33
34//Binary search, find inflection point
35//Runtime: 8 ms, faster than 61.21% of C++ online submissions for Find Minimum in Rotated Sorted Array.
36//Memory Usage: 10.3 MB, less than 34.15% of C++ online submissions for Find Minimum in Rotated Sorted Array.
37//time: O(logN)
38//space: O(1)
39class Solution {
40public:
41 int findMin(vector<int>& nums) {
42 int n = nums.size();
43
44 if(n == 1) return nums[0];
45
46 /*
47 choose nums[0] as pivot,
48 so we don't need to put nums[0] in the search range
49 */
50 int l = 0, r = n-1;
51
52 //array is sorted, not rotated
53 if(nums[l] < nums[r]) return nums[l];
54
55 int mid;
56
57 while(l <= r){
58 mid = (l+r) >> 1;
59 // cout << mid << endl;
60
61 /*
62 found the inflection point:
63 the position where right element < left element
64 */
65 if(nums[mid+1] < nums[mid]) return nums[mid+1];
66 if(nums[mid] < nums[mid-1]) return nums[mid];
67
68 if(nums[mid] < nums[0]){
69 r = mid-1;
70 }else{
71 //nums[mid] > nums[0]
72 l = mid+1;
73 }
74 }
75
76 return -1;
77 }
78};
Cost