Problem
FAANG · LeetCode #33

Search in Rotated Sorted Array — three-stage drill

1The ProblemMedium

Search in Rotated Sorted Array

Find a target's index in a sorted array that's been rotated.

rotated sorted — find index of450617203142546↑ rotation pivot (wraps here)4 is at index 6return6
Examples
[5,6,7,0,1,2,4], t=4→ 6t=8→ -1

constraintDistinct values; the array is a sorted array rotated at one pivot.

2The PatternTIME O(log n) · SPACE O(1)

Binary Search

Halve the search space each step — check the middle, keep the half that can hold the answer.

check the middle, discard the half that can't hold the target1031527394115136157left›right↑ mid
Problems using this pattern
3The SolutionRESULT → 6

Step through it

target = 450617203142546leftright↩ ANSWERresult =?
rotated sorted array · target = 4 · left = 0, right = 6
Step 1 / 4
Arrow keys to navigate · escape to reset

All problems by pattern

Backtracking

Recursive Descent