当前位置:网站首页>875. 爱吃香蕉的珂珂 - 力扣(LeetCode)
875. 爱吃香蕉的珂珂 - 力扣(LeetCode)
2022-07-06 09:28:00 【你好_Ä】
875. 爱吃香蕉的珂珂 - 力扣(LeetCode)
力扣2022/6/7每日一题
珂珂喜欢吃香蕉。这里有 n 堆香蕉,第 i 堆中有 piles[i] 根香蕉。警卫已经离开了,将在 h 小时后回来。
珂珂可以决定她吃香蕉的速度 k (单位:根/小时)。每个小时,她将会选择一堆香蕉,从中吃掉 k 根。如果这堆香蕉少于 k 根,她将吃掉这堆的所有香蕉,然后这一小时内不会再吃更多的香蕉。
珂珂喜欢慢慢吃,但仍然想在警卫回来前吃掉所有的香蕉。
返回她可以在 h 小时内吃掉所有香蕉的最小速度 k(k 为整数)。
示例 1:
输入:piles = [3,6,7,11], h = 8
输出:4
提示:
1 <= piles.length <= 10^4
piles.length <= h <= 10^9
1 <= piles[i] <= 10^9
问题解析
基础的二分答案问题,二分枚举可能的k,看k能否在规定的时间内吃完所有香蕉,如果可以说明我们速度还能继续小,如果不可以则说明我们速度要增加。
至于判断能否在规定时间内吃完香蕉,可以用枚举的mid遍历数组,如果能整除当前元素,说明吃完这一堆香蕉所用时间是(当前元素/mid),不能整除就再++。根据遍历数组后计数器的值和h的大小来判断,小于等于h就说明我们能在规定内吃完,反之不行。
AC代码
class Solution {
public:
bool check(int mid,int h,vector<int>& piles)
{
int res=0,n=piles.size();
for(int i=0;i<n;i++)
{
if(piles[i]%mid!=0)res++;
res+=piles[i]/mid;
}
return res<=h;
}
int minEatingSpeed(vector<int>& piles, int h) {
int l=1,r=1e9;
while(l<r)
{
int mid=(l+r)/2;
if(check(mid,h,piles))r=mid;
else l=mid+1;
}
return l;
}
};
边栏推荐
猜你喜欢
QT实现圆角窗口
Programmers, what are your skills in code writing?
7-1 understand everything (20 points)
渗透测试 ( 3 ) --- Metasploit Framework ( MSF )
渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具
B - Code Party (girls' competition)
Information security - threat detection engine - common rule engine base performance comparison
Basic Q & A of introductory C language
Advancedinstaller安装包自定义操作打开文件
渗透测试 2 --- XSS、CSRF、文件上传、文件包含、反序列化漏洞
随机推荐
QT模拟鼠标事件,实现点击双击移动拖拽等
滲透測試 ( 1 ) --- 必備 工具、導航
Information security - threat detection - detailed design of NAT log access threat detection platform
PySide6 信号、槽
AcWing:第58场周赛
Penetration test (3) -- Metasploit framework (MSF)
Auto.js入门
Flask框架配置loguru日志库
b站 實時彈幕和曆史彈幕 Protobuf 格式解析
QT实现窗口渐变消失QPropertyAnimation+进度条
Penetration test (2) -- penetration test system, target, GoogleHacking, Kali tool
socket通讯
Flag framework configures loguru logstore
C language learning notes
Gartner: five suggestions on best practices for zero trust network access
Raspberry pie csi/usb camera uses mjpg to realize web camera monitoring
【高老师UML软件建模基础】20级云班课习题答案合集
(POJ - 2739) sum of constructive prime numbers (ruler or two points)
Web based photo digital printing website
Interesting drink