Binary search
Binary Search
in a **sorted array**
search index
Two types:
shrink, have upper and lower bound
expand, as a multiplication
Shrinkage Template
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/description/
Option 1: left <= right
, left = mid + 1
, right = mid - 1
Option 2: stop at left and right point, then compare 2 points. left + 1 < right
, right = mid;left = mid;
Expand Template
https://leetcode.com/problems/powx-n/description/
Binary Serarch in Matrix
similar idea:
Double binary search
while (left + 1e-6 < right) https://leetcode.com/problems/minimize-max-distance-to-gas-station/description/ https://leetcode.com/problems/k-th-smallest-prime-fraction https://leetcode.com/problems/maximum-average-subarray-ii/description/
Last updated