• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode875.爱吃香蕉的珂珂|KokoEatingBananas

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10600685.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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 Kbananas, 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 = 8
Output: 4

Example 2:

Input: piles = 5
Output: 30

Example 3:

Input: piles = 6
Output: 23 

Note:

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

珂珂喜欢吃香蕉。这里有 N 堆香蕉,第 i 堆中有 piles[i] 根香蕉。警卫已经离开了,将在 H 小时后回来。

珂珂可以决定她吃香蕉的速度 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 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
iOS学习笔记43-Swift(三)类发布时间:2022-07-13
下一篇:
【Swift 2.2】iOS开发笔记(三)发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap