Find the Smallest Divisor Given a Threshold
Find the Smallest Divisor Given a Threshold (LeetCode 1283): binary search the divisor using ceiling-division sums, O(n log max).
958 published notes across every category.
Find the Smallest Divisor Given a Threshold (LeetCode 1283): binary search the divisor using ceiling-division sums, O(n log max).
Binary search on a function: find the maximum of a unimodal function by comparing neighbouring values — a discrete alternative to ternary search.
Count of Smaller Numbers After Self (LeetCode 315): scan right to left and binary-search each value into a sorted list to count smaller elements.
Time Based Key-Value Store (LeetCode 981): store sorted (timestamp, value) pairs per key and binary-search the latest value at or before a query time.
Guess Number Higher or Lower (LeetCode 374): classic binary search against a guess API in O(log n).
H-Index II (LeetCode 275): binary search on sorted citations to find the h-index in O(log n).
Find Right Interval (LeetCode 436): sort interval starts and binary-search the smallest start >= each interval end, O(n log n).
Random Pick with Weight (LeetCode 528): build prefix sums and binary-search a random target for O(log n) weighted sampling.
Lower bound vs upper bound binary search explained: first index >= target vs first index > target, with ready templates in Python, Java and C++.
The "binary search on the answer" pattern explained: when the answer space has a monotone yes/no predicate, binary-search the answer itself. With a re...
Bubble sort explained simply: repeatedly swap adjacent out-of-order items, with an early-exit optimisation. O(n^2) time, stable, in Python, Java and C...
Selection sort explained: repeatedly pick the smallest remaining element and place it next. O(n^2) time with the fewest swaps, in Python, Java and C++...