Binary Search on the Answer
In short: when "is this value good enough?" flips from no to yes exactly once as the value grows, you can binary search the answer itself, not just a sorted list. Pick a range you know contains the answer, write a yes/no feasibility check, prove in one sentence that it's monotone, then halve the range with the usual template. A range of a billion costs about 30 checks — the search is never the bottleneck, the check is.

Quarry splits every table into shards, and each shard holds at most c rows.
A table with r rows therefore needs shards, and the
cluster has room for a fixed budget of shards across all tables. Big shards are
slow to move and rebalance, so the capacity tuner wants the smallest c that
still fits every table into the budget.
There's no sorted list here to search, and there's no formula that gives c
directly, because the ceilings get in the way. But look at the output. As c
grows, the shard count never goes up. So the question "does capacity c fit in
6 shards?" reads no, no, no, …, yes, yes, yes as c increases. It flips
exactly once. Anything that flips exactly once can be binary searched.
You've done this once already
In the binary search lesson, grid_side found the biggest square grid of album
covers, and first_true found the first day a track crossed a million plays.
Neither searched a list. Both searched a range of possible answers with a
yes/no test. This lesson turns that trick into a method you can apply on purpose.
It has four questions, and you should answer them in a comment before writing the
loop:
- What is the answer space? Pick
loandhiso that the answer is certainly inside.himust be a value you know works. Herec = max(tables)puts every table in one shard, so if any capacity fits, that one does. - What is the test? A function
fits(c)that says yes or no. This is often called the feasibility check, and it's usually a simple greedy or counting loop. - Is it monotone, and which way? No-then-yes means you want the first yes (a smallest capacity). Yes-then-no means you want the last yes (a largest gap, a biggest grid).
- What does one test cost? The search makes about tests over a range of size , so the total is .