Problem 313847 · medium · Level 03 Linear Management & Searching

Slowest Pace to Finish the Shelf

binary search on the answer · greedy

You have a shelf of books; pages[i] is the length of book i. You pick a reading pace p (pages per hour) and then, every hour, read up to p pages from a single book. If fewer than p pages remain in that book, you finish it and the rest of the hour is lost. Return the smallest integer pace that lets you finish every book within hours hours.

Examples

Input:  pages = [30, 110, 230, 40], hours = 5
Output: 115
Explanation: at 115 pages/hour the books take 1 + 1 + 2 + 1 = 5 hours; at 114 they take 6.

Input:  pages = [30, 110, 230, 40], hours = 4
Output: 230

Input:  pages = [10], hours = 10
Output: 1

Constraints

  • 1 <= len(pages) <= 10**4, len(pages) <= hours <= 10**9
  • 1 <= pages[i] <= 10**9
  • Required time: O(n log(max(pages))). Trying every pace from 1 upward is far too slow.

Goals

  • Turn 'minimum value that works' into a search over a numeric range
  • Write a feasibility check using ceiling division
Starting Python…