Search a 2D Matrix II
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).
Arrays, Strings, Sliding Window, Two Pointers, Searching & Binary Search, Sorting, Linked List, Stack & Queue, Recursion & Backtracking, Trees, Heap / Priority Queue, Graphs, Dynamic Programming, Greedy, Bit Manipulation, Trie, Math & Number Theory, Design (LLD / OOP), SQL, System Design (HLD)
180 notes in 20 topics
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).
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).