InferenceAI

Enter at least two characters to search.

Start free

Minimum Reading Speed

mediumBinary Search25 min25 XP

pages[i] is the length of book ii. Each hour you read at most speed pages from a single book; if a book has fewer pages left, you finish it and wait for the next hour. Return the smallest integer speed that finishes all books within hours hours.

Hours needed at a given speed vv:

H(v)=∑i⌈piv⌉H(v) = \sum_i \left\lceil \frac{p_i}{v} \right\rceil

Constraints

  • len(pages) ≤\le hours ≤109\le 10^9

Examples

EXAMPLE WALKTHROUGHclassic

A guided view of the published example, not a trace of your code.

INPUT[[3,6,7,11],8]
TASKminimum_reading_speed()
EXPECTED OUTPUT4

Start with the input values shown in this example.

Example 1 of 3

Input: pages = [3,6,7,11], hours = 8

Output: 4

Explanation: classic

Input: pages = [30,11,23,4,20], hours = 5

Output: 30

Explanation: one hour per book

Input: pages = [30,11,23,4,20], hours = 6

Output: 23

Explanation: one spare hour

Hint 1

H(v) only decreases as v grows, so binary search on v.

Hint 2

Compute ceilings with (p + v - 1) // v.

Topics

DSABinary SearchPython