[Swift]LeetCode875. 愛吃香蕉的珂珂 | Koko Eating Bananas

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-fbbowvhz-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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.git

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 Kbananas, she eats all of them instead, and won't eat any more bananas during this hour.github

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. less

Example 1:ide

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

Example 2:this

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

Example 3:spa

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

Note:code

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

珂珂喜歡吃香蕉。這裏有 N 堆香蕉,第 i 堆中有 piles[i] 根香蕉。警衛已經離開了,將在 H 小時後回來。htm

珂珂能夠決定她吃香蕉的速度 K (單位:根/小時)。每一個小時,她將會選擇一堆香蕉,從中吃掉 K 根。若是這堆香蕉少於 K 根,她將吃掉這堆的全部香蕉,而後這一小時內不會再吃更多的香蕉。  

珂珂喜歡慢慢吃,但仍然想在警衛回來前吃掉全部的香蕉。

返回她能夠在 H 小時內吃掉全部香蕉的最小速度 KK 爲整數)。 

示例 1:

輸入: piles = [3,6,7,11], H = 8
輸出: 4

示例 2:

輸入: piles = [30,11,23,4,20], H = 5
輸出: 30

示例 3:

輸入: piles = [30,11,23,4,20], H = 6
輸出: 23 

提示:

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

340ms
 1 class Solution {
 2     func minEatingSpeed(_ piles: [Int], _ H: Int) -> Int {
 3         var minSpeed = 1, maxSpeed = 1000_00
 4         while minSpeed < maxSpeed {
 5             let speed = (maxSpeed + minSpeed) / 2
 6             if possible(piles, speed, H) { maxSpeed = speed }
 7             else { minSpeed = speed + 1}
 8         }
 9         return minSpeed
10     }
11 
12     func possible(_ piles: [Int], _ speed: Int, _ H: Int) -> Bool {
13         var time = 0
14         for p in piles { time += (p-1)/speed + 1 }
15         return H >= time
16     }
17 }

Runtime: 420 ms
Memory Usage: 19.1 MB
 1 class Solution {
 2     func minEatingSpeed(_ piles: [Int], _ H: Int) -> Int {
 3         var l:Int = 1
 4         var r:Int = 1000_000_000
 5         while (l < r)
 6         {
 7             var m:Int = (l + r) / 2
 8             var total:Int = 0
 9             for p in piles
10             {
11                 total += (p + m - 1) / m
12             }
13             if total > H
14             {
15                 l = m + 1
16             }
17             else
18             {
19                 r = m
20             }
21         }
22         return l
23     }
24 }

444ms

 1 class Solution {
 2     func minEatingSpeed(_ piles: [Int], _ H: Int) -> Int {
 3         guard piles.count > 0 else {
 4             return 0
 5         }
 6         var r = piles.max()! + 1
 7         if piles.count == H {
 8             return r - 1
 9         }
10         var l = 1
11         while l < r {
12             let m = l + (r - l) / 2
13             ///get how many hours using m
14             var hours = 0
15             for p in piles {
16                 hours += (p + m - 1) / m
17             }
18             if hours <= H {
19                 r = m
20             } else {
21                 l = m + 1
22             }
23         }
24         return l
25     }
26 }

456ms

 1 class Solution {
 2     func isGood(_ piles: [Int], _ H: Int, _ K: Int) -> Bool {
 3         var total = 0
 4         for item in piles {
 5             if item % K == 0 {
 6                 total =  total + item / K
 7             } else {
 8                 total =  total + item / K + 1
 9             }
10             if total > H {
11                 return false
12             }
13         }
14         return true
15     }
16     
17     func minEatingSpeed(_ piles: [Int], _ H: Int) -> Int {
18         if piles.count == 0 || H <= 0{
19             return 0
20         }
21         
22         var end = piles[0]
23         for item in piles {
24             end = max(end, item)
25         }
26         
27         var begin = 1
28         while(begin<=end){
29             let middle = (begin+end)/2
30             if isGood(piles, H, middle) {
31                 end = middle - 1
32             } else {
33                 begin = middle + 1
34             }
35         }
36         return begin
37     }
38 }
相關文章
相關標籤/搜索