Solve Leetcode in Rust: Koko Eating Bananas
Problem
Problem: Koko Eating Bananas
Let’s draw some pictures to understand this quesiton. If there are 4 piles of bananas like below, the given array would be [3, 2, 4, 1]
.
Then if Koko can eat 3 bananas per hour, it will takes her 5 hours to eat all the bananas like blow.
Now, if the limit hours h
is 5 then 3 bananas per hour satisfies. However, if h
, for example, is 3, then Koko need to SPEEDS UP to finish all the bananas. On the other hand, if h
is 10 which means we still have time right? So Koko should SLOW DOWN to certain speed to make herself chill but still can finish all the bananas in time.
So what is the best eating speed is the question!
Thoughts
- The maximum of speed should equals to the number of the bananas of the tallest pile
- The minimun of speed should equals to 1
Which means we are trying to find the best from 1
~ piles.max()
. Searching a value from a sorted array… maybe binary search is a soslution.
Code
Try #1
1 | use std::cmp::Ordering; |