Slowest Pace That Still Finishes

MediumBinary Search≈ 35 min

You have to read a book's chapters before an exam that is hours hours away. Chapter i has pages[i] pages. You choose a reading pace of p pages per hour, and each hour you read one chapter only: if a chapter is finished partway through an hour, the rest of that hour is spent resting, not on the next chapter.

So a chapter of c pages takes c / p hours, rounded up. Return the slowest whole-number pace that gets you through every chapter within hours hours.

Examples

text
pages = [3, 6, 7, 11], hours = 8          →  4
pages = [30, 11, 23, 4, 20], hours = 5    →  30
pages = [30, 11, 23, 4, 20], hours = 6    →  23
pages = [5], hours = 10                   →  1

In the first example, at 4 pages an hour the chapters take 1, 2, 2 and 3 hours: 8 in all. At 3 pages an hour they would take 1, 2, 3 and 4 hours: 10, too many.

Constraints

  • Between 1 and 10,000 chapters, each between 1 and 1,000,000,000 pages; hours is at least the number of chapters, and at most 1,000,000,000.
  • Aim for O(n log m), m the longest chapter: checking one pace is O(n), and the number of checks should grow only like log m.
Sound off. Turn on for key clicks and a chime when the tests pass.Inline suggestions off. Recommended while you are learning. Turn on to complete words as you type.
Console
›Run to see what your code prints

Run checks the 4 visible cases here, in your browser. Submit runs all 8 on the judge, 4 of them hidden, and measures how your solution scales.