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 ...
随机推荐
- redhat安装opencv2.4.13
1.官网下载OpenCV2.4.5 http://opencv.org/ 解压到home/用户名/opencv2.4.5 2.安装cmake $sudo apt-get install cmake ...
- Jquery 简单实现demo
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- DBeaver导入SQL脚本数据
DBeaver导入SQL脚本数据 起因 Navicat Premium还原备份的导致数据库中文乱码 看Navicat Premium中看中文数据,是正常显示了,但是在IEDA查询和Web页面上显示,均 ...
- weboack5webpack5用url-loader(file-loader)处理字体
file-loader(url-loader)可以用解析打包字体. webpack配置loader \\ webpack.config.js const webpack = require(" ...
- Redis实战(二)Redis 的 RDB 配置和数据恢复
RDB 配置解释 在 redis.conf 文件中,默认有 RDB 持久化配置: save 900 1 save 300 10 save 60 10000复制复制失败复制成功 解释: 这些配置称为检查 ...
- sqoop mysql2hive
a./etc/profile添加export HADOOP_CLASSPATH=$HADOOP_CLASSPATH:$HIVE_HOME/lib/*b.将hive-site.xml 拷贝到 $SQOO ...
- 斐波那契数python实现迭代循环两种方法
#递归方法 def fibona(n): if n == 0: return 0 elif n==1: return 1 else: return fibona(n - 1) + fibona(n - ...
- gitlab+jenkins配置自动触发构建
1.jenkins安装gitlab插件 2.启动gitlab容器 docker run -itd -v /opt/gitlab/etc:/etc/gitlab -v /opt/gitlab/log:/ ...
- RecyclerView setHasFixedSize(true)
RecyclerView setHasFixedSize(true)当recycleview大小高宽不变的时候使用这个,可以提升效率
- 解决Maven下载依赖慢的问题
参考:https://blog.csdn.net/web15085599741/article/details/126459039 <repositories> <repositor ...