当前位置:网站首页>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;
}
};
边栏推荐
- Opencv learning log 27 -- chip positioning
- C basic grammar
- What is the difficulty of programming?
- Codeforces Round #799 (Div. 4)A~H
- Problem - 1646C. Factorials and Powers of Two - Codeforces
- antd upload beforeUpload中禁止触发onchange
- Codeforces Round #800 (Div. 2)AC
- Analysis of protobuf format of real-time barrage and historical barrage at station B
- AcWing——第55场周赛
- [exercise-7] crossover answers
猜你喜欢
The concept of C language array
Maximum product (greedy)
Openwrt source code generation image
pytorch提取骨架(可微)
1013. Divide the array into three parts equal to and
Penetration test (2) -- penetration test system, target, GoogleHacking, Kali tool
Pyside6 signal, slot
[exercise-7] crossover answers
Ball Dropping
渗透测试 ( 3 ) --- Metasploit Framework ( MSF )
随机推荐
C language is the watershed between low-level and high-level
Vs2019 initial use
Differential (one-dimensional, two-dimensional, three-dimensional) Blue Bridge Cup three body attack
渗透测试 ( 3 ) --- Metasploit Framework ( MSF )
HDU - 6024 building shops (girls' competition)
分享一个在树莓派运行dash应用的实例。
AcWing——第55场周赛
[exercise-7] crossover answers
Generate random password / verification code
[exercise-7] (UVA 10976) fractions again?! (fraction split)
Information security - security professional name | CVE | rce | POC | Vul | 0day
AcWing:第56场周赛
Socket communication
QT按钮点击切换QLineEdit焦点(含代码)
Penetration testing (5) -- a collection of practical skills of scanning King nmap and penetration testing tools
Codeforces - 1526C1&&C2 - Potions
Auto.js入门
[exercise-8] (UVA 246) 10-20-30== simulation
The "sneaky" new asteroid will pass the earth safely this week: how to watch it
Nodejs crawler