Monotone Function, O(N log N)
When the array itself isn't what we search, but the answer is. If a candidate value works, every larger (or every smaller) value also works - that monotonicity lets us binary-search the answer space and replace a scan with a feasibility(mid) check. Each check costs O(N), and we run log(range) of them.
Split Input Array
Guess a capacity / size / threshold, then greedily verify it can partition the array within the allowed number of pieces.
1011. Capacity To Ship Packages Within D Days
We don't binary-search over an array index - we binary-search over the answer itself: the ship's capacity. This works because feasibility is monotone: if we can ship every package within days days at capacity m, we can also ship them at any capacity > m. So the valid capacities form a contiguous suffix and we want its leftmost element.
The condition function:
canNotShip(mid)greedily simulates loading at capacitymid: keep adding packages onto the current day until the next one would overflow, then start a new day.- If a single package is heavier than
mid, it can never ship, so it returnsTrue. - It returns
Truewhenmidis too small (total_days > days), which is exactly the "go right" signal for the lower-bound template.
The search range:
- Lower bound is
max(weights)- the belt must at least carry the heaviest single package. - Upper bound is
sum(weights)- that capacity ships everything in one day.
- Time
- O(N log S)
Nis the number of packages,Sis the sum of all weights.- Binary search over the capacity range takes
log Ssteps; each step scans allNpackages. - Space
- O(1)
- Only scalar counters are used.
410. Split Array Largest Sum
Instead of searching an index, we binary-search over the answer itself: the largest allowed subarray sum (the threshold). Feasibility is monotone: if the array can be split into m subarrays each with sum <= threshold, then it can also be split with any threshold > threshold. So the valid thresholds form a contiguous suffix and we want its leftmost element.
The condition function:
canNotHold(mid)greedily packs numbers into the current subarray until adding the next one exceedsmid, then starts a new split.- It returns
Truewhenmidis too small (splits > m), which is the "go right" signal for the lower-bound template.
The search range:
- Lower bound is
max(nums)- any threshold below the largest element is infeasible. - Upper bound is
sum(nums)- that threshold keeps everything in one subarray.
This mirrors 1011. Capacity To Ship Packages Within D Days almost exactly.
- Time
- O(2N + N log S)
Nis the length ofnums,Sis the sum of all numbers.max(nums)andsum(nums)are each a separateO(N)pass to set uploandhi-N + Ncollapses to2N.- The
while lo < hibinary search takeslog Ssteps, and each step'scanNotHold(mid)scans allNnumbers -N log S. - Space
- O(1)
- Only the scalar counters
total,splits,lo,hi, andmidare used.
875. Koko Eating Bananas
Here we don't binary-search over an array index - we binary-search over the answer itself: the eating speed k. This works because the feasibility test is monotone: if Koko can finish within h hours at speed k, she can also finish at any speed > k. So the set of valid speeds is a contiguous suffix [answer, max(piles)], and we want its leftmost element.
The condition function:
canNotEat(mid)greedily computes the hours needed at speedmid: each pile takesceil(bananas / mid)hours, written branch-free as((bananas - 1) // mid) + 1.- It returns
Truewhenmidis too slow (hours > h), which is exactly the "go right" signal for the lower-bound template.
The search range:
- Lower bound is
1(must eat at least one banana per hour). - Upper bound is
max(piles)- any faster wastes time since only one pile is eaten per hour.
- Time
- O(N + N log M)
Nis the number of piles,Mis the largest pile.max(piles)is oneO(N)pass to sethi.- Binary search over
[1, M]takeslog Msteps, and each step'scanNotEat(mid)scans allNpiles -N log M. - Space
- O(1)
- Only scalar counters are used.
1482. Minimum Number of Days to Make m Bouquets
We binary-search over the answer itself: the number of days to wait. Feasibility is monotone: if we can make m bouquets after waiting d days, we can also make them after waiting any number of days > d. So the valid wait-times form a contiguous suffix and we want its leftmost element.
The condition function:
canNotBloom(mid)scans the flowers in order. A flower has bloomed ifbloomDay <= mid. We greedily collect runs ofkadjacent bloomed flowers into one bouquet; any unbloomed flower resets the current run.- It returns
Truewhenmiddays are not enough (bouqets < m), which is the "go right" signal for the lower-bound template.
The search range:
- Lower bound is
1. - Upper bound is
max(bloomDay), by which every flower has bloomed. - If
len(bloomDay) < m * k, there aren't enough flowers and the answer is-1.
- Time
- O(N log M)
Nis the length of the array,Mis the maximum value inbloomDay.- Binary search over
[1, M]takeslog Msteps; each step scans allNflowers. - Space
- O(1)
- Only scalar counters are used.
1231. Divide Chocolate
1231Divide Chocolate
We binary-search over the answer itself: the minimum piece sweetness we're willing to keep. We split the bar into k + 1 contiguous pieces and want to maximize the minimum piece sum. Feasibility is monotone (an inverted version of the usual shape): if a target minimum x is achievable, then x - 1 is also achievable - making the bar easier to cut into enough pieces. So the valid targets form a contiguous prefix and we want its rightmost element.
The condition function:
canNotDivide(mid)greedily accumulates sweetness, cutting a new piece whenever the running sum exceedsmid, and counts how many such cuts (splits) are possible.- It returns
Truewhenmidis achievable (splits > k), pushingloup to find the largest workable minimum.
The search range:
- Lower bound is
min(sweetness). - Upper bound is
sum(sweetness)- keep the entire bar as one piece.
- Time
- O(N log S)
Nis the length of the array,Sis the sum of all sweetness values.- Binary search over the sweetness range takes
log Ssteps; each step scans allNchunks. - Space
- O(1)
- Only scalar counters are used.
More problems built on the same "guess the answer, verify greedily" shape:
- 774. Minimize Max Distance to Gas Station (Hard)
- 1891. Cutting Ribbons (Medium)
- 2226. Maximum Candies Allocated to K Children (Medium)
- 2064. Minimized Maximum of Products Distributed to Any Store (Medium)
- 2137. Pour Water Between Buckets to Make Water Levels Equal (Medium)
- 2387. Median of a Row Wise Sorted Matrix (Medium)
Kth Smallest
Binary-search the value, and let the feasibility check count how many elements fall at or below mid.