当前位置:网站首页>875. Leetcode, a banana lover
875. Leetcode, a banana lover
2022-07-06 16:14:00 【Hello_ Ä】
875. Coco, who loves bananas - Power button (LeetCode)
Power button 2022/6/7 A daily topic
Coco likes bananas . Here you are n Make bananas , The first i There is... In the pile piles[i] A banana . The guards have left , Will be in h Come back in hours .
Coco can decide how fast she eats bananas k ( Company : root / Hours ). Every hour , She will choose a bunch of bananas , Eat from it k root . If this pile of bananas is less than k root , She will eat all the bananas in this pile , Then I won't eat more bananas in an hour .
Coco likes to eat slowly , But still want to eat all the bananas before the guards come back .
She can go back to h Minimum speed to eat all bananas in hours k(k Integers ).
Example 1:
Input :piles = [3,6,7,11], h = 8
Output :4
Tips :
1 <= piles.length <= 10^4piles.length <= h <= 10^91 <= piles[i] <= 10^9
Problem analysis
Basic binary answer question , Binary enumeration possible k, see k Can you finish eating all bananas within the specified time , If you can explain that our speed can continue to be small , If not, it means that our speed should be increased .
As for judging whether you can finish eating bananas within the specified time , Enumerable mid Traversal array , If you can divide the current element , It means that the time taken to finish eating this pile of bananas is ( The current element /mid), If you can't divide it, then ++. According to the sum of the counter value after traversing the array h To judge the size of , Less than or equal to h It means that we can finish within the specified time , Not vice versa .
AC Code
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;
}
};
边栏推荐
- Advancedinstaller安装包自定义操作打开文件
- Find 3-friendly Integers
- 875. 爱吃香蕉的珂珂 - 力扣(LeetCode)
- 860. Lemonade change
- 生成随机密码/验证码
- Differential (one-dimensional, two-dimensional, three-dimensional) Blue Bridge Cup three body attack
- Information security - Analysis of security orchestration automation and response (soar) technology
- Codeforces Round #800 (Div. 2)AC
- socket通讯
- Configuration du cadre flask loguru log Library
猜你喜欢

807. Maintain the urban skyline
frida hook so层、protobuf 数据解析

628. Maximum product of three numbers

b站 實時彈幕和曆史彈幕 Protobuf 格式解析

605. Planting flowers

Codeforces Round #799 (Div. 4)A~H

antd upload beforeUpload中禁止触发onchange

Borg maze (bfs+ minimum spanning tree) (problem solving report)

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

Web based photo digital printing website
随机推荐
window11 conda安装pytorch过程中遇到的一些问题
Educational Codeforces Round 130 (Rated for Div. 2)A~C
图图的学习笔记-进程
Differential (one-dimensional, two-dimensional, three-dimensional) Blue Bridge Cup three body attack
useEffect,函數組件掛載和卸載時觸發
(POJ - 3579) median (two points)
日期加1天
Sword finger offer II 019 Delete at most one character to get a palindrome
E. Breaking the Wall
Analysis of protobuf format of real-time barrage and historical barrage at station B
C basic grammar
input 只能输入数字,限定输入
Programmers, what are your skills in code writing?
1689. Ten - the minimum number of binary numbers
Useeffect, triggered when function components are mounted and unloaded
树莓派4B安装opencv3.4.0
Maximum product (greedy)
Information security - Analysis of security orchestration automation and response (soar) technology
Generate random password / verification code
1323. Maximum number of 6 and 9