Binary Search Searching the answer
241. Koko Eating Bananas
You have a pile of books to read before a deadline. Book i has pages[i] pages, and you have hours hours
in all. You read at a fixed speed: a whole number of pages per hour that you choose before you start, the
same for every hour.
Each hour, you pick one unfinished book and read up to speed pages of it. If fewer pages than that are left,
you finish the book and rest for the remainder of the hour. You start another book only when the next hour
begins. So a book of 10 pages takes 3 hours at 4 pages an hour.
Return the smallest speed that lets you finish every book within hours hours.
Example 1
Input: pages = [12, 7, 25, 18], hours = 7
12Explanation: At 12 pages an hour the books take 1, 1, 3 and 2 hours, which is 7 hours in all. At 11 pages an hour the first book takes 2 hours instead of 1, and the total becomes 8.
Example 2
Input: pages = [5, 9, 3], hours = 3
9Explanation: There are as many hours as books, so each book must be finished in a single hour. The longest book has 9 pages, so the speed must be at least 9.
Example 3
Input: pages = [1000000000], hours = 3
333333334Explanation: At 333333333 pages an hour, 3 hours cover 999999999 pages, one page short of the book, so a fourth hour is needed. One more page an hour is enough.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ pages.length ≤ 1051 ≤ pages[i] ≤ 109pages.length ≤ hours ≤ 109
Clarifying questions
What you would ask an interviewer before writing any code.