LeetCode – 875. Koko Eating Bananas



LeetCode – 875. Koko Eating Bananas

Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.

Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.

Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.

Return the minimum integer K such that she can eat all the bananas within H hours.

Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4

Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30

Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23

Note:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9

题意:

Koko是一只喜欢吃香蕉的大猩猩。现在有N堆香蕉,第i堆有piles[i]个香蕉。管理员现在离开了,会在H(H >= piles.length)小时之后回来。Koko可以决定吃香蕉的速度,即每小时吃K个香蕉。每个小时它选择一堆香蕉,并吃掉其中的K个香蕉。如果某一堆里剩下的香蕉少于K个,它会吃完这一堆剩下的所有香蕉,但它这一小时内不会吃更多的其他堆的香蕉。

假设Koko喜欢慢慢地吃香蕉,但又想在管理员回来之前吃完所有香蕉。请问Koko每小时至少需要吃多少香蕉才能在管理员回来之前吃完呢?

例如,有4堆香蕉,每堆香蕉的数目为[3, 6, 7,11],如果管理员在8小时后回来,那么Koko每小时吃4个香蕉。

分析:

由于Koko在一个小时内把一堆香蕉吃完之后不会再去吃其他的香蕉,那么它一小时能吃掉的香蕉的数目不会超过最多的一堆香蕉的数目(记为M)。同时,它每小时最少会吃1个香蕉,所以最终Koko决定的吃香蕉的速度K应该是在1到M之间。

我们可以应用二分查找的思路,先选取1和M的平均数,(1+M)/2,看以这个速度Koko能否在H小时内吃掉所有香蕉。如果不能在H小时内吃掉所有的香蕉,那么它需要尝试更快的速度,也就是K应该在(1+M)/2到M之间,下一次我们尝试(1+M)/2和M的平均值。

如果Koko以(1+M)/2的速度能够在H小时内吃完所有的香蕉,那么我们来判断这是不是最慢的速度。可以尝试一下稍微慢一点的速度,(1+m)/2 - 1。如果Koko以这个速度不能在H小时之内吃完所有香蕉,那么(1+M)/2就是最慢的可以在H小时吃完香蕉的速度。如果以(1+m)/2 - 1的速度也能在H小时内吃完香蕉,那么接下来Koko尝试更慢的速度,1和(1+M)/2的平均值。

以此类推,我们按照二分查找的思路总能找到让Koko在H小时内吃完所有香蕉的最慢速度K。以下是这种思路的参考代码:


Read full article from LeetCode – 875. Koko Eating Bananas


No comments:

Post a Comment

Labels

Algorithm (219) Lucene (130) LeetCode (97) Database (36) Data Structure (33) text mining (28) Solr (27) java (27) Mathematical Algorithm (26) Difficult Algorithm (25) Logic Thinking (23) Puzzles (23) Bit Algorithms (22) Math (21) List (20) Dynamic Programming (19) Linux (19) Tree (18) Machine Learning (15) EPI (11) Queue (11) Smart Algorithm (11) Operating System (9) Java Basic (8) Recursive Algorithm (8) Stack (8) Eclipse (7) Scala (7) Tika (7) J2EE (6) Monitoring (6) Trie (6) Concurrency (5) Geometry Algorithm (5) Greedy Algorithm (5) Mahout (5) MySQL (5) xpost (5) C (4) Interview (4) Vi (4) regular expression (4) to-do (4) C++ (3) Chrome (3) Divide and Conquer (3) Graph Algorithm (3) Permutation (3) Powershell (3) Random (3) Segment Tree (3) UIMA (3) Union-Find (3) Video (3) Virtualization (3) Windows (3) XML (3) Advanced Data Structure (2) Android (2) Bash (2) Classic Algorithm (2) Debugging (2) Design Pattern (2) Google (2) Hadoop (2) Java Collections (2) Markov Chains (2) Probabilities (2) Shell (2) Site (2) Web Development (2) Workplace (2) angularjs (2) .Net (1) Amazon Interview (1) Android Studio (1) Array (1) Boilerpipe (1) Book Notes (1) ChromeOS (1) Chromebook (1) Codility (1) Desgin (1) Design (1) Divide and Conqure (1) GAE (1) Google Interview (1) Great Stuff (1) Hash (1) High Tech Companies (1) Improving (1) LifeTips (1) Maven (1) Network (1) Performance (1) Programming (1) Resources (1) Sampling (1) Sed (1) Smart Thinking (1) Sort (1) Spark (1) Stanford NLP (1) System Design (1) Trove (1) VIP (1) tools (1)

Popular Posts