100 XP

86. Slowest eating speed

Advanced searching

There are piles of snacks on the table, and the guests arrive in hours hours. You eat at a steady speed of speed snacks per hour, like this: each hour you pick one pile and eat up to speed snacks from it. If the pile has fewer than that, you finish it and wait for the next hour before starting another pile.

So at a speed of 4, a pile of 10 takes 3 hours (4, 4, then the last 2), and a pile of 3 takes 1 hour.

Write a function slowest_speed(piles, hours) that takes the list of pile sizes and the number of hours, and returns the slowest whole-number speed that finishes every pile within hours hours.

  • There are always at least as many hours as piles, so a fast enough speed always exists.
  • Piles can hold up to 10^9 snacks, so trying speeds 1, 2, 3, ... one at a time is far too slow.

Example 1

Inputpiles = [5, 9, 2], hours = 6
Output3
Explanation2 + 3 + 1 = 6 hours. At 2 it takes 9.

Example 2

Inputpiles = [10], hours = 3
Output4
Explanation4, 4, 2: three hours. At 3 it takes four.

Example 3

Inputpiles = [4, 4, 4], hours = 3
Output4
ExplanationOne hour per pile means eating 4 an hour.

Constraints

  • piles has 1 to 100 whole numbers, each from 1 to 10^9.
  • hours is a whole number from len(piles) to 10^9.

Submit also runs 9 more tests you can't see.

Code
def slowest_speed(piles, hours):
    """Find the slowest speed that eats every pile in time.

    piles: list of int, the size of each pile
    hours: int, the hours available, at least len(piles)
    Returns: int, the smallest speed (snacks per hour)
        that finishes within hours
    """
    # Write your code here
    pass