leetcode 875. 爱吃香蕉的珂珂
珂珂喜欢吃香蕉。这里有 n 堆香蕉,第 i 堆中有 piles[i] 根香蕉。警卫已经离开了,将在 h 小时后回来。
珂珂可以决定她吃香蕉的速度 k (单位:根/小时)。每个小时,她将会选择一堆香蕉,从中吃掉 k 根。如果这堆香蕉少于 k 根,她将吃掉这堆的所有香蕉,然后这一小时内不会再吃更多的香蕉。
珂珂喜欢慢慢吃,但仍然想在警卫回来前吃掉所有的香蕉。
返回她可以在 h 小时内吃掉所有香蕉的最小速度 k(k 为整数)。
示例 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 <= 104
piles.length <= h <= 109
1 <= piles[i] <= 109
思路:使用二分法,取值从1-N,防止溢出,mid = (low+high)/2可以转换为int mid = (high - low) / 2 + low; 对pile/speed向上取整可以转换为(pile + speed - 1) / speed;注意total+的范围为long.
二分实现更新:
class Solution {
public:
long GetMax(vector<int> &piles, int k)
{
long time = 0;
for (auto i : piles) {
time += (i + k - 1) / k;
}
return time;
}
int GetMaxId(vector<int> &piles, int h)
{
int low = 1;
int high = 0;
for (auto i : piles) {
high = max(i, high);
}
int res = high;
while (low <= high) {
int mid = (high - low) / 2 + low;
long timeCnt = GetMax(piles, mid);
if (timeCnt > h) {
low = mid + 1;
} else {
high = mid - 1;
res = mid;
}
}
return res;
}
int minEatingSpeed(vector<int> &piles, int h)
{
return GetMaxId(piles, h);
}
};
代码:
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;
long GetMax(vector<int> &piles, int k)
{
long time = 0;
for (auto i : piles) {
time += ceil(i / (k * 1.0)); // (pile + speed - 1) / speed;
}
return time;
}
int GetMaxId(vector<int> &piles, int h)
{
int low = 1;
int high = 0;
for (auto i : piles) {
high = max(i, high);
}
int res = high;
while (low < high) {
int mid = (high + low) / 2; //int speed = (high - low) / 2 + low;
long timeCnt = GetMax(piles, mid);
if (timeCnt > h) {
low = mid + 1;
} else {
high = mid;
res = mid;
}
}
return res;
}
int minEatingSpeed(vector<int> &piles, int h)
{
return GetMaxId(piles, h);
}
int main()
{
vector<int> piles = { 312884470 };
int h = 968709470;
int res = minEatingSpeed(piles, h);
cout << "result: " << res << endl;
return 0;
}
leetcode 875. 爱吃香蕉的珂珂的更多相关文章
- [leetcode] 875. 爱吃香蕉的珂珂(周赛)
875. 爱吃香蕉的珂珂 这题时间要求比较严格... 首先,将piles排序,然后二分查找. 总之,答案K肯定位于piles[?]piles[?+1]或者1piles[0]之间 所以我们先二分把?找到 ...
- Leetcode之二分法专题-875. 爱吃香蕉的珂珂(Koko Eating Bananas)
Leetcode之二分法专题-875. 爱吃香蕉的珂珂(Koko Eating Bananas) 珂珂喜欢吃香蕉.这里有 N 堆香蕉,第 i 堆中有 piles[i] 根香蕉.警卫已经离开了,将在 H ...
- [Swift]LeetCode875. 爱吃香蕉的珂珂 | Koko Eating Bananas
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i]bananas. The gu ...
- [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 g ...
- 【python游戏编程之旅】第五篇---嗷大喵爱吃鱼小游戏开发实例
本系列博客介绍以python+pygame库进行小游戏的开发.有写的不对之处还望各位海涵. 我们一同在前几期的博客中已经学到了很多pygame的基本知识了,现在该做个小游戏实战一下了. 前几期博客链接 ...
- CDOJ1927 爱吃瓜的伊卡洛斯(2) 【并查集】启发式合并+set
伊卡洛斯很爱吃西瓜.一次,他来到一个西瓜摊旁,发现水果摊有N个西瓜,西瓜有红色.黄色.绿色.蓝色……等等数不清的颜色. 伊卡洛斯很想知道知道一些信息,便于老板交谈了起来. 当老板的话的第一个字符为”A ...
- codevs 2277 爱吃皮蛋的小明(水题日常)
时间限制: 1 s 空间限制: 32000 KB 题目等级 : 白银 Silver 题目描述 Description 小明特别爱吃蛋,特别是皮蛋.他一次可以吃一个蛋或者两个蛋(整个吞下去),而且他 ...
- 猴猴吃香蕉 背包DP
猴猴吃香蕉 背包DP \(D\)次询问,第\(i\)次询问,每次有\(n_i\)个带权香蕉,问有多少方案使香蕉之积为\(k_i\),对结果取模\(1000000007\) \(n\le 10^3,k\ ...
- [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 g ...
- JarvisOJ Basic 爱吃培根的出题人
听说你也喜欢吃培根?那我们一起来欣赏一段培根的介绍吧: bacoN is one of aMerICa'S sWEethEartS. it's A dARlinG, SuCCulEnt fOoD tH ...
随机推荐
- 12组-Beta冲刺-总结
组长博客链接 https://www.cnblogs.com/147258369k/p/15615820.html 一.基本情况 1.1 现场答辩总结 柯老师意见: 面向普通用户群体推出排行榜功能 增 ...
- wget 多线程下载 mwget 工具安装
# for centos mini #yum -y install gcc-c++ intltool libssl-dev yum -y install gcc-c++ intltool openss ...
- 怎么才能卸载inventor?完全彻底卸载删除干净inventor各种残留注册表和文件的方法和步骤
怎么才能卸载inventor?完全彻底卸载删除干净inventor各种残留注册表和文件的方法和步骤.如何卸载inventor呢?有很多同学想把inventor卸载后重新安装,但是发现inventor安 ...
- .Net的产品变迁
从开始接触.Net以来,转眼过了好几年.在这许多年里,不得不感慨.Net产品线也在不断发生着变化, 下面就简单的进行一下梳理. .Net能做桌面应用程序,也就是C/S,也能做网站开发,就是通常说的B/ ...
- 抓包工具Fidder
第一步:直接在浏览器下载 第二步:设置Fiddler打开Fiddler, Tools-> Fiddler Options (配置完后记得要重启Fiddler)选中"Decrpt ...
- String和StringBuffer与StringBuilder的区别
1.String.StringBuffer.StringBuilder都不可以被继承,在JDK中它们都被定义为final类. 2.执行速度:StringBuilder > StringBuffe ...
- pip修改镜像源
# 单独一次下载软件 pip install markdown -i https://pypi.tuna.tsinghua.edu.cn/simple # 全局修改 pip config set gl ...
- ERA5气压层数据驱动WRF的一些问题
感谢Dawn的建议,兰溪之水的WRF教程 参考了一些经验,并结合实际后,成功用ERA5驱动WRF.实际上,用ERA5数据驱动WRF的方法和用ERA-Interim 数据驱动WRF极其类似. 总结几点是 ...
- Error building Player because scripts had compiler errors
报错信息如下: Error building Player because scripts had compiler errors Build completed with a result of ' ...
- base64 转图片
data:image+jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD//gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcg% ...