当前位置:网站首页>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^4piles.length <= h <= 10^91 <= 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;
}
};
边栏推荐
- b站 实时弹幕和历史弹幕 Protobuf 格式解析
- [analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class
- D - function (HDU - 6546) girls' competition
- 【练习-9】Zombie’s Treasure Chest
- 1529. Minimum number of suffix flips
- [exercise-3] (UVA 442) matrix chain multiplication
- 1855. Maximum distance of subscript alignment
- Configuration du cadre flask loguru log Library
- Penetration test (2) -- penetration test system, target, GoogleHacking, Kali tool
- Codeforces Round #801 (Div. 2)A~C
猜你喜欢

Penetration test (8) -- official document of burp Suite Pro

Analysis of protobuf format of real-time barrage and historical barrage at station B

QT实现窗口置顶、置顶状态切换、多窗口置顶优先关系

Flask框架配置loguru日志库

Gartner: five suggestions on best practices for zero trust network access

Write web games in C language

409. Longest palindrome

2027. Minimum number of operations to convert strings

1005. Maximized array sum after K negations
frida hook so层、protobuf 数据解析
随机推荐
滲透測試 ( 1 ) --- 必備 工具、導航
Nodejs crawler
628. Maximum product of three numbers
1529. Minimum number of suffix flips
渗透测试 ( 7 ) --- 漏洞扫描工具 Nessus
1013. Divide the array into three parts equal to and
双向链表—全部操作
Information security - Analysis of security orchestration automation and response (soar) technology
Programmers, what are your skills in code writing?
Data storage in memory & loading into memory to make the program run
The "sneaky" new asteroid will pass the earth safely this week: how to watch it
807. Maintain the urban skyline
C language is the watershed between low-level and high-level
969. Pancake sorting
快速转 TypeScript 指南
[exercise-3] (UVA 442) matrix chain multiplication
QT模拟鼠标事件,实现点击双击移动拖拽等
C language learning notes
[exercise-6] (PTA) divide and conquer
力扣——第298场周赛