当前位置:网站首页>Force deduction solution summary 875- coco who likes bananas
Force deduction solution summary 875- coco who likes bananas
2022-06-12 02:12:00 【Lost summer】
Directory links :
Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog
GitHub Synchronous question brushing items :
https://github.com/September26/java-algorithms
Original link :
describe :
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
Example 2:
Input :piles = [30,11,23,4,20], h = 5
Output :30
Example 3:
Input :piles = [30,11,23,4,20], h = 6
Output :23
Tips :
1 <= piles.length <= 10^4
piles.length <= h <= 109
1 <= piles[i] <= 109
source : Power button (LeetCode)
link :https://leetcode.cn/problems/koko-eating-bananas
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking :
* Their thinking : * The simplest way to solve this problem , We can 1 Start trying , Try until you reach the maximum value in the array . But the time complexity is too high , Can cause a timeout . * So the way to solve this problem is binary search , Set up left and right, If sum No timeout , be right Move to the left , whereas left To the right . Finally, finding the critical point is the result we want .
Code :
public int minEatingSpeed(int[] piles, int h) {
int right = 0;
for (int i : piles) {
right = Math.max(i, right);
}
if (piles.length == h) {
return right;
}
int result = 1;
int left = 1;
while (true) {
int middle = (left + right) / 2;
if (left > right) {
break;
}
long sum = 0;
for (int i : piles) {
sum += i / middle;
if (i % middle != 0) {
sum++;
}
}
if (sum <= h) {
result = middle;
right = middle + (right - middle) / 2 - 1;
} else {
left = middle + 1;
}
}
return result;
}边栏推荐
- Graphical data analysis | data analysis tool map
- 力扣解法汇总824-山羊拉丁文
- Graphical data analysis | business analysis and data mining
- leetcode:6. Zigzag transformation
- 阿里云oss文件上传系统
- Force deduction solution summary 473 match to square
- ozzanimation-基于sse的动作系统
- MySQL table common operation mind map
- Linux(CentOS6)安装MySQL5.5版本数据库
- Smartbi helps you solve the problem of losing high-value customers
猜你喜欢

MySQL表常用操作思维导图

通用树形结构的迭代与组合模式实现方案

Does the virtual host have independent IP

MySQL高级部分知识点

Almost all schools will ask for the second round exam! Come in and recite the answer!

How to stop anti-virus software from blocking a web page? Take gdata as an example

ozzanimation-基於sse的動作系統

Installing MySQL version 5.5 database for Linux (centos6)

CVPR2022 | iFS-RCNN:一种增量小样本实例分割器
![[adjustment] in 2022, the Key Laboratory of laser life sciences of the Ministry of education of South China Normal University enrolled adjustment students in optics, electronic information, biomedicin](/img/f9/332b206d5aca0ca6afc3fdf11a53c8.jpg)
[adjustment] in 2022, the Key Laboratory of laser life sciences of the Ministry of education of South China Normal University enrolled adjustment students in optics, electronic information, biomedicin
随机推荐
力扣解法汇总821-字符的最短距离
Summary of force deduction solution 944- deletion of sequence
Force deduction solution summary 868- binary spacing
Graphical data analysis | business analysis and data mining
Force deduction solution summary 824 goat Latin
Installing MySQL version 5.5 database for Linux (centos6)
C asynchronous programming from simple to deep (III) details awaiter
Summary of concrete (ground + wall) + Mountain crack data set (classification and target detection)
Add sequence number column to MySQL query result set
How WPS inserts a directory and the operating steps for quickly inserting a directory
力扣解法汇总473-火柴拼正方形
What are the advantages of adaptive search advertising?
Force deduction solution summary 386 dictionary order
Modification of system module information of PHP security development 12 blog system
Database
MySQL table common operation mind map
打包一个包含手表端应用的手机端APK应用—Ticwear
Linux(CentOS7)安裝MySQL-5.7版本
Ozzanation - système d'action basé sur sse
Don't miss it! Five large data visualization screens that HR must collect