Minimum Grading Speed

Medium

This is an extra practice problem. It doesn't have walkthrough slides. You can solve it with the pattern template you already know. Open the hints if you get stuck.

Question

You're a teacher with several stacks of ungraded exams on your desk. Each stack holds some number of exams. You'll grade at a steady rate of k exams per hour. You finish one stack completely before you start the next.

Grading a stack always takes a whole number of hours. If a stack still has exams left after an hour, you spend a full extra hour on that same stack. Any time left over in that hour doesn't carry over to the next stack.

You're given the stacks and the number of hours before grades are due. Return the minimum rate k that lets you finish every stack in time.

Input: stacks = [10, 20, 30], hours = 6

Output: 10

At a rate of 10, grading takes 1 + 2 + 3 = 6 hours. That just fits. A rate of 9 would take 2 + 3 + 4 = 9 hours. That is too slow.

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

Output: 4

At a rate of 4, grading takes 1 + 2 + 2 + 3 = 8 hours. A rate of 3 would take 1 + 2 + 3 + 4 = 10 hours. That misses the deadline.

Input: stacks = [1, 1, 1, 1], hours = 4

Output: 1

There are exactly as many hours as stacks. So even the slowest rate of 1 exam per hour is enough.

You might also hear this problem called “Koko Eating Bananas.”

Clarify the problem

What are some questions you'd ask an interviewer?

Understand the problem

Given these inputs, what is the minimum grading rate? stacks = [5, 12, 9], hours = 6
4
5
6
7

Remember to understand the problem and plan your approach before you start coding.