当前位置:网站首页>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^4
piles.length <= h <= 10^9
1 <= 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;
}
};
边栏推荐
- Penetration test (4) -- detailed explanation of meterpreter command
- The "sneaky" new asteroid will pass the earth safely this week: how to watch it
- Alice and Bob (2021 Niuke summer multi school training camp 1)
- [exercise-1] (UVA 673) parentheses balance/ balanced brackets (stack)
- 生成随机密码/验证码
- B - Code Party (girls' competition)
- 渗透测试 2 --- XSS、CSRF、文件上传、文件包含、反序列化漏洞
- Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs
- 1605. Sum the feasible matrix for a given row and column
- [teacher Gao UML software modeling foundation] collection of exercises and answers for level 20 cloud class
猜你喜欢
Codeforces Round #797 (Div. 3)无F
Data storage in memory & loading into memory to make the program run
Sword finger offer II 019 Delete at most one character to get a palindrome
快速转 TypeScript 指南
渗透测试 ( 1 ) --- 必备 工具、导航
[exercise-5] (UVA 839) not so mobile (balance)
滲透測試 ( 1 ) --- 必備 工具、導航
Pytorch extract skeleton (differentiable)
Codeforces Round #799 (Div. 4)A~H
969. Pancake sorting
随机推荐
Pytorch extract skeleton (differentiable)
1903. Maximum odd number in string
Quick to typescript Guide
Interval sum ----- discretization
Codeforces Round #799 (Div. 4)A~H
Codeforces Round #800 (Div. 2)AC
2027. Minimum number of operations to convert strings
[exercise-6] (PTA) divide and conquer
Useeffect, triggered when function components are mounted and unloaded
Borg maze (bfs+ minimum spanning tree) (problem solving report)
QT有关QCobobox控件的样式设置(圆角、下拉框,向上展开、可编辑、内部布局等)
F - birthday cake (Shandong race)
图图的学习笔记-进程
Bidirectional linked list - all operations
Information security - security professional name | CVE | rce | POC | Vul | 0day
Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
Socket communication
[exercise-7] crossover answers
Openwrt source code generation image
Sanic异步框架真的这么强吗?实践中找真理