MemotivaLeetCode Patterns Flashcards: Binary Search, Search Space, Templates, Edge Cases

What is binary search on the answer space and how does it work?

LeetCode Patterns Flashcards: Binary Search, Search Space, Templates, Edge Cases

Аудио-карточка · 0:31

Nortren·

What is binary search on the answer space and how does it work?

0:31

Instead of searching within the input array, binary search on the answer space searches for the optimal value in a range of possible answers. Define the minimum and maximum possible answers, then binary search for the boundary where a feasibility check function transitions from possible to impossible. For example, "minimum capacity to ship within d days" has answers ranging from the maximum package weight to the total weight sum. For each candidate capacity at the midpoint, greedily check if shipping within d days is feasible. If feasible, try a smaller capacity; if not, try a larger one.
neetcode.io