← Home

33. Search in Rotated Sorted Array

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
binary searchC++Markdown
33

A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 33. Search in Rotated Sorted Array, the solution in this repository is mainly a binary search solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. 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, sliding window.

The notes already sitting in the source point us in the right direction:

  • binary search
  • https://leetcode.com/problems/search-in-rotated-sorted-array/discuss/14425/Concise-O(log-N)-Binary-search-solution

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are findMinIdx, search.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • 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:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. 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) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//binary search
02//https://leetcode.com/problems/search-in-rotated-sorted-array/discuss/14425/Concise-O(log-N)-Binary-search-solution
03class Solution {
04public:
05    int findMinIdx(vector<int>& nums){
06        int l = 0, r = nums.size()-1;
07        
08        while(l <= r){
09            int mid = l + (r-l)/2;
10            if(nums[mid] > nums.back()){
11                //ex: [4,5,6,7,0,1,2], 7 > 2
12                //so search in right part
13                l = mid+1;
14            }else{
15                //right part is monotonic,
16                //so search in left part
17                r = mid-1;
18            }
19        }
20        
21        return l;
22    };
23    
24    int search(vector<int>& nums, int target) {
25        int n = nums.size();
26        if(n == 0) return -1;
27        int minIdx = findMinIdx(nums);
28        if(nums[minIdx] == target) return minIdx;
29        
30        // cout << "minIdx: " << minIdx << endl;
31        
32        //this is the important part!!
33        int l, r;
34        if(nums[n-1] >= target){
35            //we can find target in left part
36            l = minIdx + 1;
37            r = n-1;
38        }else{
39            //we can find target in left part
40            l = 0;
41            r = minIdx-1;
42        }
43        
44        while(l <= r){
45            int mid = l + (r-l)/2;
46            
47            // cout << l << " " << mid << " " << r << endl;
48            
49            if(nums[mid] == target){
50                return mid;
51            }else if(nums[mid] > target){
52                r = mid-1;
53            }else{
54                l = mid+1;
55            }
56        }
57        
58        return -1;
59    }
60};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.