Find the Slowest Reading Speed That Still Finishes All Books in Time

Solve this Problem
Medium20–25 min
Topics
Companies
Practice:LeetCode ↗GFG ↗
Given an array pages where each value is the number of pages in one book, and an integer hours, find the slowest possible reading speed (in whole pages per hour) that still finishes every book within the time limit. You read one book at a time, always at the same constant integer speed. A book with p pages, read at speed s, takes ⌈p / s⌉ hours — even one leftover page still costs a full extra hour, since you can't carry unfinished pages into the next book's count. Return the minimum integer speed that keeps the total hours across every book at or under hours — a solution is always guaranteed to exist as long as there are at least as many hours as books. The total hours needed only ever decreases (or stays flat) as the reading speed increases, which makes this a binary search on the answerBinary Search on the AnswerInstead of searching a sorted array, the search runs directly over the space of possible answers (here, every candidate reading speed). It works whenever "is this candidate good enough?" is monotonic — once a candidate works, every larger (or smaller, depending on direction) candidate keeps working too. — search directly over candidate speeds rather than over the books themselves.

Test Case 1:

Input:pages = [12, 20, 17, 9], hours = 10
Output:7
Explanation:At a speed of 7 pages/hour the books take 2 + 3 + 3 + 2 = 10 hours — exactly the limit, and no slower speed fits.

Test Case 2:

Input:pages = [7], hours = 3
Output:3
Explanation:A single 7-page book read at 3 pages/hour takes ⌈7/3⌉ = 3 hours.

Test Case 3:

Input:pages = [25, 14, 33, 8, 19], hours = 12
Output:10
Explanation:At speed 10, the five books take 3 + 2 + 4 + 1 + 2 = 12 hours.

Constraints

  • 1 ≤ number of books ≤ 10⁴
  • 1 ≤ pages in a single book ≤ 10⁹
  • number of books ≤ hours ≤ 10⁹
🚀

Try the Dry Run

Don't just read the solution — watch it execute, one step at a time.

Approach & Solutions

Brute Force — Try Every Speed From 1 Upward

Brute

Reading at speed s, a book with p pages takes ⌈p / s⌉ hours (any leftover pages still cost a full extra hour). Try s = 1, 2, 3, ... and for each one add up the hours every book would take; the first speed whose total fits within the limit is the answer, since a faster speed can only ever need the same or fewer hours than a slower one. Correct, but checking every candidate speed one at a time is wasteful once the page counts get large.

TimeO(n · maxPages)
SpaceO(1)
1class Solution { 2 public int minReadingSpeed(int[] pages, int hours) { 3 int maxPages = 0; 4 for (int p : pages) maxPages = Math.max(maxPages, p); 5 for (int speed = 1; speed <= maxPages; speed++) { 6 long total = 0; 7 for (int p : pages) total += (p + speed - 1) / speed; 8 if (total <= hours) return speed; 9 } 10 return maxPages; 11 } 12}

Optimal — Binary Search on the Speed

Optimal

The total hours needed only ever goes down (or stays the same) as the speed goes up — that monotonic relationship is exactly what binary search needs. Search the candidate speeds from 1 to the largest single book: whenever a candidate speed's total hours fits the limit, it's a valid answer, so record it and try a slower (smaller) speed to see if that still works; otherwise the speed is too slow, so search faster.

TimeO(n · log(maxPages))
SpaceO(1)
1class Solution { 2 public int minReadingSpeed(int[] pages, int hours) { 3 int lo = 1, hi = 0; 4 for (int p : pages) hi = Math.max(hi, p); 5 int ans = hi; 6 while (lo <= hi) { 7 int mid = lo + (hi - lo) / 2; 8 long total = 0; 9 for (int p : pages) total += (p + mid - 1) / mid; 10 if (total <= hours) { 11 ans = mid; 12 hi = mid - 1; 13 } else { 14 lo = mid + 1; 15 } 16 } 17 return ans; 18 } 19}

Related Problems