Slowest speed that finishes
medium
New here? How this page works
- Read the Problem tab. Read it twice. Look at the examples: what goes in, what should come out.
- Open Break it down. Answer the questions in your own words. This is your plan.
- Write code on the right. Start small. Stuck on a blank page? Press “Write my plan as comments”.
- Press Run examples. See which examples pass. Red ones show what your code gave and what was expected.
- Stuck? Use Hints, one at a time. Still stuck after all of them? Read the Walkthrough. That's learning, not cheating.
- Press Submit. It runs hidden tests too. Pass them all, and it's solved.
You have a stack of books, and pages[i] is the number of pages in book i. You have hours hours to read them all.
You read at a steady speed of s pages per hour. Each hour, you work on one book only: if you finish a book partway through an hour, you rest until the next hour starts. So a book of 7 pages at speed 3 takes 3 hours (3, then 3, then the last 1).
Return the slowest whole-number speed that lets you finish every book within hours.
Examples
Example 1
- Input
- pages = [4, 9, 2, 13], hours = 9
- Output
- 4
At 4 pages an hour: 1 + 3 + 1 + 4 = 9 hours. At 3 it would take 2 + 3 + 1 + 5 = 11.
Example 2
- Input
- pages = [25, 8, 17, 30, 12], hours = 5
- Output
- 30
5 books in 5 hours means one hour per book, so you must read the biggest (30 pages) in an hour.
Example 3
- Input
- pages = [25, 8, 17, 30, 12], hours = 7
- Output
- 17
At 17 pages an hour: 2 + 1 + 1 + 2 + 1 = 7 hours.
Rules
pageshas between 1 and 10,000 books, each with 1 to 1,000,000,000 pages.hoursis at least the number of books (so a fast enough speed always works) and at most 1,000,000,000.
Words on this page, in plain English (1)Open if any word is new to you
- Stack
- A pile where you only touch the top: add with
push, take withpop. The last thing in is the first out, like a pile of plates.
Answer each question in your own words before you look at ours. Rough notes are fine: the point is to think before you type code. Your answers stay in this browser.
1.What goes in?
Name every input and what it looks like. Can it be empty? Sorted? Negative?
Stuck? Each hint gives a little more away. Try the problem again after each one.
This explains the whole solution.
You will learn the most by trying first, even if you don’t finish. But if this pattern is new to you, reading the walkthrough first is a fine way to learn it.
1. Understand the problem
At speed s, a book of p pages takes p ÷ s hours, rounded up: a book of 7 pages at 3 per hour takes 3 hours, because you can't start the next book in the leftover part of the last hour. We want the slowest speed whose total time fits in hours.
2. Solve an example by hand
Books [4, 9, 2, 13], 9 hours. At speed 3: 2 + 3 + 1 + 5 = 11 hours, too slow. At speed 4: 1 + 3 + 1 + 4 = 9 hours, fits. So the answer is 4.
Ask "does speed s finish in time?" for each speed: 1 no, 2 no, 3 no, 4 yes, 5 yes, 6 yes… It flips from no to yes once, because reading faster never takes longer.
3. The slow way
Try speeds 1, 2, 3… until one works. Each try adds up every book (O(n)), and the answer could be a billion: O(n × biggest book). Far too slow for big books.
4. Spot the pattern
There's no sorted list, but there is a yes/no question that flips once as the speed grows. We want the first yes: binary search on the answer. The "list" is the speeds from 1 to the biggest book.
Why is the biggest book enough as the top? At that speed, every book takes exactly 1 hour, and the problem promises hours is at least the number of books.
5. The idea
- Write a helper: hours needed at a given speed.
- Binary search the speeds for the first one that fits, exactly like Where it would go: if
midfits, the answer ismidor slower (high = mid); if not, it's faster (low = mid + 1).
Binary search over reading speeds
pages
possible speeds
- hours allowed =
- 9
- hours needed at mid =
- –
function slowestSpeed(pages, hours) { let low = 1; let high = Math.max(...pages); // reading the biggest book in an hour always works while (low < high) { const mid = Math.floor((low + high) / 2); if (hoursNeeded(pages, mid) <= hours) { high = mid; // fast enough: the answer is mid or slower } else { low = mid + 1; // too slow } } return low;} function hoursNeeded(pages, speed) { let total = 0; for (const book of pages) { total += Math.ceil(book / speed); // a partly read hour still counts } return total;}The answer is between 1 page an hour and 13 (the biggest book), which always works.
6. The code, line by line
let low = 1; let high = Math.max(...pages); // reading the biggest book in an hour always worksThe answer is somewhere from 1 to the biggest book.
Math.max(...pages)spreads the list intoMath.maxto find the biggest.while (low < high) { const mid = Math.floor((low + high) / 2); if (hoursNeeded(pages, mid) <= hours) { high = mid; // fast enough: the answer is mid or slower } else { low = mid + 1; // too slow }The "first yes" binary search: if
midis fast enough, keep it in the range as a possible answer; otherwise everything up tomidis too slow.return low;When the range is one speed, it's the slowest that works.
function hoursNeeded(pages, speed) { let total = 0; for (const book of pages) { total += Math.ceil(book / speed); // a partly read hour still counts } return total;}The helper.
Math.ceilrounds up, because a partly read hour still uses up the whole hour.
7. How fast is it?
| Try every speed | Binary search | |
|---|---|---|
| Time | O(n × biggest book) | O(n × log(biggest book)): about 30 tries × n |
| Extra memory | O(1) | O(1) |
This is the shape of many "find the minimum that works" problems: write a check for one candidate, then binary search the candidates.
Common mistakes
- Rounding down.
Math.floor(7 / 3)is 2, but reading 7 pages at 3 per hour takes 3 hours. - Searching from 0. A speed of 0 means dividing by zero. Start at 1.
- Searching for "exactly
hours". The fastest speed might finish early. We want the slowest that fits, not an exact match.
Memorise the solution, once you understand it.
This shows the full solution, then helps you rebuild it from memory in four steps. Remembering it is what lets you write it again in an interview, or in a week. Your result goes into your reviews, so it comes back just before you'd forget.
Learn it by heart: Slowest speed that finishes
Read the 20 lines slowly. For each one, say out loud what it does and why it is there. Then cover it and move on to step 2.
function slowestSpeed(pages, hours) { let low = 1; let high = Math.max(...pages); // reading the biggest book in an hour always works while (low < high) { const mid = Math.floor((low + high) / 2); if (hoursNeeded(pages, mid) <= hours) { high = mid; // fast enough: the answer is mid or slower } else { low = mid + 1; // too slow } } return low;} function hoursNeeded(pages, speed) { let total = 0; for (const book of pages) { total += Math.ceil(book / speed); // a partly read hour still counts } return total;}