当前位置:网站首页>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;
}
};
边栏推荐
- 渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具
- 最全编程语言在线 API 文档
- Analysis of protobuf format of real-time barrage and historical barrage at station B
- 860. Lemonade change
- JS call camera
- Sword finger offer II 019 Delete at most one character to get a palindrome
- 渗透测试 ( 4 ) --- Meterpreter 命令详解
- Openwrt build Hello ipk
- Information security - Analysis of security orchestration automation and response (soar) technology
- Penetration test (2) -- penetration test system, target, GoogleHacking, Kali tool
猜你喜欢
Flag framework configures loguru logstore
树莓派4B安装opencv3.4.0
[exercise-7] crossover answers
渗透测试 ( 1 ) --- 必备 工具、导航
Configuration du cadre flask loguru log Library
渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具
Differential (one-dimensional, two-dimensional, three-dimensional) Blue Bridge Cup three body attack
Pytorch extract skeleton (differentiable)
【练习-7】Crossword Answers
C language learning notes
随机推荐
【练习-6】(PTA)分而治之
[exercise-8] (UVA 246) 10-20-30== simulation
[exercise-1] (UVA 673) parentheses balance/ balanced brackets (stack)
Opencv learning log 27 -- chip positioning
Openwrt source code generation image
Ball Dropping
[exercise-3] (UVA 442) matrix chain multiplication
Openwrt build Hello ipk
[exercise-7] crossover answers
[analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class
力扣——第298场周赛
Read and save zarr files
Essai de pénétration (1) - - outils nécessaires, navigation
Flask框架配置loguru日志库
QT实现圆角窗口
Opencv learning log 26 -- detect circular holes and mark them
Borg maze (bfs+ minimum spanning tree) (problem solving report)
Truck History
The "sneaky" new asteroid will pass the earth safely this week: how to watch it
Socket communication