A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 81. Search in Rotated Sorted Array II, the solution in this repository is mainly a binary search solution.
Guide
What?
The first job is to translate the English prompt into state, transition, and stopping conditions. 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, bit manipulation.
The notes already sitting in the source point us in the right direction:
- time: O(N), space: O(1)
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are findMinIdx, search, isBinarySearchHelpful, isInFirst.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- 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:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
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(N), space: O(1)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 8 ms, faster than 95.64% of C++ online submissions for Search in Rotated Sorted Array II.
02//Memory Usage: 14.1 MB, less than 31.59% of C++ online submissions for Search in Rotated Sorted Array II.
03//time: O(N), space: O(1)
04class Solution {
05public:
06 int findMinIdx(vector<int>& nums){
07 /*
08 it seems it's hard to find min_idx in
09 [2,2,2,0,2,2] using binary search
10 so here we use O(n) method to find min_idx
11 */
12
13 auto min_it = min_element(nums.begin(), nums.end());
14
15 int min_idx = min_it - nums.begin();
16
17 if(min_idx == 0 && nums[0] == nums.back()){
18 int i = nums.size()-1;
19 while(i >= 0 && nums[0] == nums[i]){
20 --i;
21 }
22 min_idx = i+1;
23 }else{
24 int i = min_idx-1;
25 while(i >= 0 && nums[i] == nums[min_idx]){
26 --i;
27 }
28 min_idx = i+1;
29 }
30
31 return min_idx;
32 };
33
34 bool search(vector<int>& nums, int target) {
35 int n = nums.size();
36 if(n == 0) return false;
37 int minIdx = findMinIdx(nums);
38 if(nums[minIdx] == target) return true;
39
40 // cout << "minIdx: " << minIdx << endl;
41
42 //this is the important part!!
43 int l, r;
44 if(nums[n-1] >= target){
45 //we can find target in left part
46 l = minIdx + 1;
47 r = n-1;
48 }else{
49 //we can find target in left part
50 l = 0;
51 r = minIdx-1;
52 }
53
54 while(l <= r){
55 int mid = l + (r-l)/2;
56
57 // cout << l << " " << mid << " " << r << endl;
58
59 if(nums[mid] == target){
60 return true;
61 }else if(nums[mid] > target){
62 r = mid-1;
63 }else{
64 l = mid+1;
65 }
66 }
67
68 return false;
69 }
70};
71
72//Approach 1: Binary Search
73//Runtime: 8 ms, faster than 95.64% of C++ online submissions for Search in Rotated Sorted Array II.
74//Memory Usage: 14.2 MB, less than 12.12% of C++ online submissions for Search in Rotated Sorted Array II.
75//time: O(N), space: O(1)
76class Solution {
77public:
78 bool isBinarySearchHelpful(vector<int>& nums, int l, int ele){
79 //if ele < nums[l], then ele is in second part
80 //if ele > nums[l], then ele is in first part
81 //if ele == nums[l], then it could exist in both parts
82 // we should use the dynamically changed "l" rather than 0!!
83 // l = 0;
84 return nums[l] != ele;
85 }
86
87 bool isInFirst(vector<int>& nums, int l, int ele){
88 // we should use the dynamically changed "l" rather than 0!!
89 // l = 0;
90 return ele >= nums[l];
91 }
92
93 bool search(vector<int>& nums, int target) {
94 int n = nums.size();
95
96 int l = 0, r = n-1;
97
98 while(l <= r){
99 int mid = l + (r-l)/2;
100
101 // cout << l << ", " << mid << ", " << r << endl;
102
103 int pivot = nums[mid];
104
105 if(pivot == target){
106 return true;
107 }
108
109 if(!isBinarySearchHelpful(nums, l, pivot)){
110 ++l;
111 continue;
112 }
113
114 bool pivotIsInFirst = isInFirst(nums, l, pivot);
115 bool targetIsInFirst = isInFirst(nums, l, target);
116
117 if(pivotIsInFirst ^ targetIsInFirst){
118 //nums[mid] and target are in different array
119 if(pivotIsInFirst){
120 //pivot in first, target in second, so search in right part
121 // cout << "p1t2" << endl;
122 l = mid+1;
123 }else{
124 // cout << "p2t1" << endl;
125 r = mid-1;
126 }
127 }else{
128 if(target > pivot){
129 // cout << "pt same, t > p" << endl;
130 l = mid+1;
131 }else{
132 // cout << "pt same, t < p" << endl;
133 r = mid-1;
134 }
135 }
136 }
137
138 return false;
139 }
140};
Cost