Sqrt(x) Using Binary Search
Compute integer Sqrt(x) (LeetCode 69) with binary search on the answer in O(log x), rounding down, in Python, Java and C++.
958 published notes across every category.
Compute integer Sqrt(x) (LeetCode 69) with binary search on the answer in O(log x), rounding down, in Python, Java and C++.
Koko Eating Bananas (LeetCode 875): binary search on the eating speed, checking feasibility in O(n log maxPile).
Capacity To Ship Packages Within D Days (LeetCode 1011): binary search the ship capacity with a greedy feasibility check, O(n log sum).
Split Array Largest Sum (LeetCode 410): binary search the largest subarray sum with a greedy split check, O(n log sum).
Median of Two Sorted Arrays (LeetCode 4): binary search a partition of the smaller array for O(log(min(m,n))) time.
Search a 2D Matrix (LeetCode 74): treat the row-sorted matrix as one sorted list and binary search in O(log(m*n)).
Search a 2D Matrix II (LeetCode 240): staircase search from the top-right corner eliminates a row or column each step, in O(m+n).
Find K Closest Elements (LeetCode 658): binary search the best window start so the k-length window is closest to x, in O(log(n-k)+k).
Kth Smallest Element in a Sorted Matrix (LeetCode 378): binary search on value, counting elements <= mid in O(n log(range)).
Aggressive Cows: binary search the largest minimum distance between cows placed in stalls, with a greedy feasibility check, O(n log range).
Allocate Minimum Number of Pages: binary search the minimum possible maximum pages per student with a greedy allocation check, O(n log sum).
Single Element in a Sorted Array (LeetCode 540): binary search on even indices to find the one non-duplicated element in O(log n).