当前位置:网站首页>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;
}
};
边栏推荐
- Web based photo digital printing website
- [exercise -11] 4 values why sum is 0 (and 4 values of 0)
- Openwrt source code generation image
- 1323. Maximum number of 6 and 9
- (POJ - 3258) River hopper (two points)
- (POJ - 3579) median (two points)
- Sword finger offer II 019 Delete at most one character to get a palindrome
- Perform general operations on iptables
- Flask框架配置loguru日志庫
- Penetration test (2) -- penetration test system, target, GoogleHacking, Kali tool
猜你喜欢

Information security - threat detection engine - common rule engine base performance comparison

Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs

Information security - Analysis of security orchestration automation and response (soar) technology

Information security - threat detection - detailed design of NAT log access threat detection platform

TCP's three handshakes and four waves

QT有关QCobobox控件的样式设置(圆角、下拉框,向上展开、可编辑、内部布局等)

树莓派4B安装opencv3.4.0
Quick to typescript Guide

2078. Two houses with different colors and the farthest distance

Advancedinstaller安装包自定义操作打开文件
随机推荐
Suffix expression (greed + thinking)
CEP used by Flink
921. Minimum additions to make parentheses valid
Information security - threat detection - detailed design of NAT log access threat detection platform
Essai de pénétration (1) - - outils nécessaires, navigation
Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs
QWidget代码设置样式表探讨
Flag framework configures loguru logstore
Hdu-6025-prime sequence (girls' competition)
Quick to typescript Guide
Openwrt build Hello ipk
frida hook so层、protobuf 数据解析
C basic grammar
C language is the watershed between low-level and high-level
Opencv learning log 29 -- gamma correction
树莓派CSI/USB摄像头使用mjpg实现网页摄像头监控
TCP's three handshakes and four waves
Information security - Epic vulnerability log4j vulnerability mechanism and preventive measures
1903. Maximum odd number in string
605. Planting flowers