Minimum Eating Speed
Updated
Problem statement
Your daughter, Alex, has just come home with a bag full of candy after a long night of trick-or-treating. Before going to sleep, Alex places the candy in numPiles piles with the i-th pile containing candyPiles[i] number of candies.
After arranging the candies into piles, Alex announces she is going to sleep for numHours hours.
Your plan is to eat all the candy before Alex wakes up in numHours. You can eat c candies per hour, but in each hour you will only eat candy from a single pile. If a pile contains fewer than c candies, you will only eat the number of candies in that pile and you will wait until the next hour to eat more candy.
Having a little bit of self-restraint your goal is to calculate the smallest number of candies c you need to eat per hour in order to finish all the candy before Alex wakes up again.
Function
minimumEatingSpeed(candyPiles: int[], numHours: int) → int
Complete the function minimumEatingSpeed in the editor.
minimumEatingSpeed has the following parameters:
int[] candyPiles: an array of integers representing the number of candies in each pileint numHours: the number of hours Alex will be asleep for
Returns
int: the smallest number of candies c that you need to eat per hour in order to finish all the candies before Alex wakes up
Examples
Example 1
candyPiles = [4, 9, 11, 17]numHours = 8return = 6To finish all the candies in 8 hours, you can eat at the following speed:
- Hour 1: Eat 6 candies from the pile with 17 candies, 11 remain.
- Hour 2: Eat 6 candies from the pile with 11 candies, 5 remain.
- Hour 3: Eat 5 candies from the pile with 5 candies, 0 remain.
- Hour 4: Eat 6 candies from the pile with 9 candies, 3 remain.
- Hour 5: Eat 3 candies from the pile with 3 candies, 0 remain.
- Hour 6: Eat 4 candies from the pile with 4 candies, 0 remain.
- Hour 7: Eat 6 candies from the pile with 11 candies, 5 remain.
- Hour 8: Eat 5 candies from the pile with 5 candies, 0 remain.
The smallest number of candies you need to eat per hour is 6.
More Airbnb interview questions
- Bouncing DiagonalsOA · Seen Dec 2026
- Count Numbers Not Equal to the First TwoOA · Seen Dec 2026
- Garden Sprinkler PlacementOA · Seen Dec 2026
- Theme Park Shuttle Wait TimeOA · Seen Dec 2026
- Merge Event PreferencesPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Run-Length Encoding and DecodingPHONE SCREEN · Seen Nov 2026
- Count Card Duel RoundsOA · Seen Nov 2026
- Count Lamps Lighting Each PointOA · Seen Nov 2026
- Simulate Orchard Rot SpreadOA · Seen Nov 2026
- Sort Adjacent PairsOA · Seen Nov 2026
Similar questions at other companies
- Minimum Non-Adjacent Selection CapabilityMicrosoft · OAMicrosoft interview questions
- Count Pairs of Overlapping SquaresCapital One · OACapital One interview questions
- Bank Accounts with Balance HistoryMeta · OAMeta interview questions
- Index of the Maximum After Left RotationsChubb · OAChubb interview questions
- Rolling MetricsAkuna Capital · OAAkuna Capital interview questions
- Longest Increasing Price SubsequenceOpenAI · OAOpenAI interview questions
- Linear InterpolatorTwo Sigma · OATwo Sigma interview questions
- Rock JumpingIMC Trading · OAIMC Trading interview questions
- Find Sum of BeautiesAmazon · OA
- Minimum Cost to Attend Required CoursesVisa · OAVisa interview questions