当前位置:网站首页>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;
}
};
边栏推荐
- Understand what is a programming language in a popular way
- Input can only input numbers, limited input
- 力扣——第298场周赛
- [exercise-6] (PTA) divide and conquer
- Flask框架配置loguru日志庫
- Educational Codeforces Round 130 (Rated for Div. 2)A~C
- Web based photo digital printing website
- 2027. Minimum number of operations to convert strings
- [exercise-2] (UVA 712) s-trees
- AcWing:第58场周赛
猜你喜欢
628. Maximum product of three numbers
Penetration test (7) -- vulnerability scanning tool Nessus
2027. Minimum number of operations to convert strings
605. Planting flowers
Basic Q & A of introductory C language
渗透测试 ( 1 ) --- 必备 工具、导航
b站 實時彈幕和曆史彈幕 Protobuf 格式解析
window11 conda安装pytorch过程中遇到的一些问题
1323. Maximum number of 6 and 9
Differential (one-dimensional, two-dimensional, three-dimensional) Blue Bridge Cup three body attack
随机推荐
(POJ - 3579) median (two points)
渗透测试 ( 3 ) --- Metasploit Framework ( MSF )
Alice and Bob (2021 Niuke summer multi school training camp 1)
frida hook so层、protobuf 数据解析
[exercise 4-1] cake distribution
1903. Maximum odd number in string
Auto. Getting started with JS
Web based photo digital printing website
(POJ - 3258) River hopper (two points)
Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs
[exercise -10] unread messages
AcWing:第58场周赛
Shell Scripting
第 300 场周赛 - 力扣(LeetCode)
The concept of C language array
F - birthday cake (Shandong race)
渗透测试 2 --- XSS、CSRF、文件上传、文件包含、反序列化漏洞
Sanic异步框架真的这么强吗?实践中找真理
Determine the Photo Position
Problem - 1646C. Factorials and Powers of Two - Codeforces