Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b3tGdV3Vm51QUZxg2x
21 lines
1.4 KiB
Plaintext
21 lines
1.4 KiB
Plaintext
Binary search on the answer: how many times the yes/no question was asked
|
|
=========================================================================
|
|
|
|
Integer square root = largest m with m*m <= x; the question is "is m*m > x?"
|
|
sqrt(8) = 2 asked 3 times, instead of trying up to 9 candidates
|
|
sqrt(16) = 4 asked 4 times, instead of trying up to 17 candidates
|
|
sqrt(1,000,000,007) = 31622 asked 30 times, instead of trying up to 1,000,000,008 candidates
|
|
sqrt(2,147,483,647) = 46340 asked 31 times, instead of trying up to 2,147,483,648 candidates
|
|
|
|
Koko eating bananas: smallest speed k with sum(ceil(pile / k)) <= h
|
|
piles=[3, 6, 7, 11] h=8 -> speed 4 asked 4 times (candidates 1..11)
|
|
piles=[30, 11, 23, 4, 20] h=5 -> speed 30 asked 4 times (candidates 1..30)
|
|
piles=[30, 11, 23, 4, 20] h=6 -> speed 23 asked 5 times (candidates 1..30)
|
|
for [3, 6, 7, 11], hours needed at speed 1..11: 27, 15, 10, 8, 8, 6, 5, 5, 5, 5, 4
|
|
is that <= 8? F F F T T T T T T T T
|
|
piles=[1000000000, 999999999, 500000000] h=10 -> speed 250,000,000 asked 29 times (candidates 1..1,000,000,000); checked: speed-1 needs 13 hours
|
|
|
|
Ship packages within D days: smallest capacity so that, loading in order, D days are enough
|
|
weights=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] days=5 -> capacity 15 asked 5 times (candidates 10..55)
|
|
days needed at capacity 15: 5; at capacity 14: 6
|