Question Bank

Binary Search on the Answer

Difficulty: Medium

Cast feasibility problems as binary search over a monotone predicate. Drills cover min capacity, max chunk, and the standard template.

Question Bank
/

Binary Search on the Answer

Binary Search on the Answer

Cast feasibility problems as binary search over a monotone predicate. Drills cover min capacity, max chunk, and the standard template.

Question Bank
Medium
Python
5 questions
binary-search
binary-search-templates
algorithms
interview-prep

1,159 views

33

You must ship weights over D days. Capacity cap is feasible if the ships can finish in D days. Write feasible(cap).

Examples

Example 1:

Input: weights = [1, 2, 3, 4, 5], D = 3, cap = 6
Output: True
Explanation: Greedy pack: [1, 2, 3] (sum 6), [4] (sum 4), [5] (sum 5). 3 ships, fits in D = 3 days.

Example 2:

Input: weights = [1, 2, 3, 4, 5], D = 3, cap = 5
Output: False
Explanation: Greedy fills [1, 2] then [3] then [4] then [5] which is 4 ships, exceeding D = 3.