当前位置:网站首页>Force buckle 875 Coco, who likes bananas
Force buckle 875 Coco, who likes bananas
2022-06-26 05:46:00 【SP FA】
This is a mathematical problem with a dichotomy . Through this problem, I learned a little skill about accuracy :
It can be seen from the meaning of the question , If we want to eat p i l e s [ i ] piles[i] piles[i] Time spent , Can pass ⌈ p i l e s [ i ] m i d ⌉ \lceil\frac{piles[i]}{mid}\rceil ⌈midpiles[i]⌉ To calculate , among m i d mid mid Is the median of two points . But this will involve the problem of accuracy when the number is very large , Such as p i l e s [ i ] = 1000000000 piles[i]=1000000000 piles[i]=1000000000, m i d = 999999986 mid=999999986 mid=999999986, At this point, use the above formula to calculate the result is 2, But actually it should be 3 That's right .
resolvent
because p i l e s [ i ] > 0 piles[i]>0 piles[i]>0, m i d > 0 mid>0 mid>0, So the original formula is equivalent to ⌊ p i l e s [ i ] + m i d − 1 m i d ⌋ \lfloor\frac{piles[i]+mid-1}{mid}\rfloor ⌊midpiles[i]+mid−1⌋, This is equivalent to... Without affecting the results p i l e s [ i ] piles[i] piles[i] It's magnified , If the number gets bigger, the result will get bigger , Therefore, the problem of accuracy is avoided . Specifically, you can substitute the above numbers into the experience .
The idea of dichotomy is very good , Don't go into details . Go straight to the code :
class Solution {
public int minEatingSpeed(int[] piles, int h) {
Arrays.sort(piles);
if (h == piles.length) {
return piles[piles.length-1];
}
int l = 1;
int r = piles[piles.length-1];
while (l < r) {
int mid = (l + r) >> 1;
int count = 0;
for (int i=0;i<piles.length;i++) {
count += (piles[i] + mid - 1) / mid;
}
if (count > h) {
l = mid + 1;
}
else r = mid;
}
return l;
}
}
边栏推荐
猜你喜欢
随机推荐
Pytorch中自己所定义(修改)的模型加载所需部分预训练模型参数并冻结
uniCloud云开发获取小程序用户openid
一段不离不弃的爱情
Bingc (inheritance)
电商借助小程序技术发力寻找增长突破口
Consul服务注册与发现
ZigBee explain in simple terms lesson 2 hardware related and IO operation
Feelings of virtual project failure
Operator priority, associativity, and whether to control the evaluation order [detailed explanation]
【ARM】讯为rk3568开发板buildroot添加桌面应用
[arm] build boa based embedded web server on nuc977
ZigBee learning in simple terms Lecture 1
怎么把平板作为电脑的第二扩展屏幕
小小面试题之GET和POST的区别
使用Jedis监听Redis Stream 实现消息队列功能
Mise en file d'attente des messages en utilisant jedis Listening redis stream
Project suspension
Something about MariaDB
Life is so fragile
【MYSQL】MySQL 百万级数据量分页查询方法及其优化







