Binary Search
Note
# Find the position where x should be inserted to maintain the sorted order of the array
# The left most
array.bisect_left(x)
# The right most
array.bisect_right(x)
Binary Search - A Different Perspective | Python Algorithms - YouTube
Types of questions
- Firstly see whether the range is
[left, right]
or[left, right)
to determine the conditions
Find first or last of something
- Outer loop is still binary search, add additional logic when reach mid condition
- See wether this mid is the first or last
- If true, return it
- Else, adjust the range
- See wether this mid is the first or last
981. Time Based Key-Value Store
3152. Special Array II
2332. The Latest Time to Catch a Bus
Minimum in a sorted array
- Find whether the potential minimum is in left half or right half
while left < right:
mid = left + (right - left) // 2
if nums[mid] < nums[right]:
right = mid
else:
left = mid + 1
return nums[left]
154. Find Minimum in Rotated Sorted Array II
Find range of something
File | confidence | date | difficulty | note |
---|---|---|---|---|
4. Median of Two Sorted Arrays | 🔴 | March 14, 2024 | hard | - |
658. Find K Closest Elements | 🔴 | April 25, 2024 | medium | Mid pointer is start of the interval, r start at length - 2. Now, compare two values, m and m + k. If m + k is closer, that means, everything before m is outside of consideration. Else, everything beyond m + k is outside of consideration. |
528. Random Pick with Weight | 🟡 | June 26, 2024 | medium | Use prefix sum, then binary search on this list of range represented by the prefix sum array |
1060. Missing Element in Sorted Array | 🔴 | August 31, 2024 | medium | Find the left most index such that between this index and start of the array, the missing number is greater or equal to k. Then, find the previous element. (Watch for edge case where missing number is after the array) |
1885. Count Pairs in Two Arrays | 🔴 | September 01, 2024 | medium | Modify the question to find x, y in sorted array such that their sum is greater than 0 |
1552. Magnetic Force Between Two Balls | 🔴 | September 02, 2024 | medium | Do binary search on the force rather than the array |
852. Peak Index in a Mountain Array | 🟡 | September 04, 2024 | medium | If reached 0 or length of array index, then it must be the peak |
540. Single Element in a Sorted Array | 🟡 | September 04, 2024 | medium | - |
400. Nth Digit | 🔴 | September 04, 2024 | medium | The number is zero indexed |
2055. Plates Between Candles | 🔴 | September 04, 2024 | medium | Use prefix sum, and position of the candles, to fine the leftmost and the right most candle in the given range |
1146. Snapshot Array | 🔴 | September 04, 2024 | medium | Turn into sub array, then find the largest in the sub array greater than the snap_id |
3152. Special Array II | 🔴 | September 18, 2024 | medium | Find the starting index of the sub special array, and then do a binary search on that |
2332. The Latest Time to Catch a Bus | 🔴 | September 18, 2024 | medium | Do binary search on the time to get on, and then verify whether the person can get on at this time or not |
154. Find Minimum in Rotated Sorted Array II | 🔴 | September 18, 2024 | medium | Consider the edge case where element at index r is equal to mid |
729. My Calendar I | 🔴 | October 07, 2024 | medium | Insert at the first False position |
1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold | 🟡 | October 07, 2024 | medium | Prefix sum |
786. K-th Smallest Prime Fraction | 🔴 | October 12, 2024 | medium | Binary search from interval 0 to 1 |
1060. Missing Element in Sorted Array
1885. Count Pairs in Two Arrays
1552. Magnetic Force Between Two Balls
2055. Plates Between Candles
540. Single Element in a Sorted Array
852. Peak Index in a Mountain Array
1146. Snapshot Array
400. Nth Digit
729. My Calendar I
1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold
File | confidence | date | difficulty | note |
---|---|---|---|---|
33. Search in Rotated Sorted Array | 🟢 | March 02, 2024 | - | - |
981. Time Based Key-Value Store | 🟢 | March 14, 2024 | - | - |
875. Koko Eating Bananas | 🟢 | March 14, 2024 | - | - |
74. Search a 2D Matrix | 🟢 | March 14, 2024 | medium | - |
4. Median of Two Sorted Arrays | 🔴 | March 14, 2024 | hard | - |
153. Find Minimum in Rotated Sorted Array | 🟢 | March 14, 2024 | - | - |
162. Find Peak Element | 🟢 | March 30, 2024 | medium | search for monotonic increase portion |
658. Find K Closest Elements | 🔴 | April 25, 2024 | medium | Mid pointer is start of the interval, r start at length - 2. Now, compare two values, m and m + k. If m + k is closer, that means, everything before m is outside of consideration. Else, everything beyond m + k is outside of consideration. |
34. Find First and Last Position of Element in Sorted Array | 🟢 | April 25, 2024 | medium | Basically the combination of finding the left most and right most index of a number |
2300. Successful Pairs of Spells and Potions | 🟢 | May 09, 2024 | medium | l <= r, mid + 1 and mid - 1, select l at the end |
528. Random Pick with Weight | 🟡 | June 26, 2024 | medium | Use prefix sum, then binary search on this list of range represented by the prefix sum array |
633. Sum of Square Numbers | 🟢 | July 07, 2024 | medium | The domain is restricted to c, then use two pointers |
1060. Missing Element in Sorted Array | 🔴 | August 31, 2024 | medium | Find the left most index such that between this index and start of the array, the missing number is greater or equal to k. Then, find the previous element. (Watch for edge case where missing number is after the array) |
1885. Count Pairs in Two Arrays | 🔴 | September 01, 2024 | medium | Modify the question to find x, y in sorted array such that their sum is greater than 0 |
1552. Magnetic Force Between Two Balls | 🔴 | September 02, 2024 | medium | Do binary search on the force rather than the array |
852. Peak Index in a Mountain Array | 🟡 | September 04, 2024 | medium | If reached 0 or length of array index, then it must be the peak |
540. Single Element in a Sorted Array | 🟡 | September 04, 2024 | medium | - |
400. Nth Digit | 🔴 | September 04, 2024 | medium | The number is zero indexed |
2055. Plates Between Candles | 🔴 | September 04, 2024 | medium | Use prefix sum, and position of the candles, to fine the leftmost and the right most candle in the given range |
1146. Snapshot Array | 🔴 | September 04, 2024 | medium | Turn into sub array, then find the largest in the sub array greater than the snap_id |
3152. Special Array II | 🔴 | September 18, 2024 | medium | Find the starting index of the sub special array, and then do a binary search on that |
2332. The Latest Time to Catch a Bus | 🔴 | September 18, 2024 | medium | Do binary search on the time to get on, and then verify whether the person can get on at this time or not |
154. Find Minimum in Rotated Sorted Array II | 🔴 | September 18, 2024 | medium | Consider the edge case where element at index r is equal to mid |
729. My Calendar I | 🔴 | October 07, 2024 | medium | Insert at the first False position |
1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold | 🟡 | October 07, 2024 | medium | Prefix sum |
786. K-th Smallest Prime Fraction | 🔴 | October 12, 2024 | medium | Binary search from interval 0 to 1 |
File | confidence | date | difficulty | note |
---|---|---|---|---|
33. Search in Rotated Sorted Array | 🟢 | March 02, 2024 | - | - |
981. Time Based Key-Value Store | 🟢 | March 14, 2024 | - | - |
875. Koko Eating Bananas | 🟢 | March 14, 2024 | - | - |
74. Search a 2D Matrix | 🟢 | March 14, 2024 | medium | - |
153. Find Minimum in Rotated Sorted Array | 🟢 | March 14, 2024 | - | - |
162. Find Peak Element | 🟢 | March 30, 2024 | medium | search for monotonic increase portion |
34. Find First and Last Position of Element in Sorted Array | 🟢 | April 25, 2024 | medium | Basically the combination of finding the left most and right most index of a number |
2300. Successful Pairs of Spells and Potions | 🟢 | May 09, 2024 | medium | l <= r, mid + 1 and mid - 1, select l at the end |
633. Sum of Square Numbers | 🟢 | July 07, 2024 | medium | The domain is restricted to c, then use two pointers |