当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢

适配器模式

DOM文档

Introduction to GUI programming to game practice (I)

pytorch(环境、tensorboard、transforms、torchvision、dataloader)

The difference between get and post in small interview questions

10 set

家庭记账程序(第二版 加入了循环)

REUSE_ ALV_ GRID_ Display event implementation (data_changed)

【ARM】讯为rk3568开发板buildroot添加桌面应用

uniCloud云开发获取小程序用户openid
随机推荐
C XX management system
【PHP】PHP二维数组按照多个字段进行排序
【MYSQL】MySQL 百万级数据量分页查询方法及其优化
一段不离不弃的爱情
421- binary tree (226. reversed binary tree, 101. symmetric binary tree, 104. maximum depth of binary tree, 222. number of nodes of complete binary tree)
12 multithreading
从新东方直播来探究下小程序音视频通话及互动直播
Ribbon负载均衡服务调用
虚拟项目失败感想
Pytorch (network model training)
使用Jedis监听Redis Stream 实现消息队列功能
The most refined language interprets the event dispatcher (also known as the event scheduler)
Could not get unknown property ‘*‘ for SigningConfig container of type org. gradle. api. internal
Learn cache lines and pseudo sharing of JVM slowly
Operator priority, associativity, and whether to control the evaluation order [detailed explanation]
状态模式,身随心变
写在父亲节前
Positioning setting horizontal and vertical center (multiple methods)
冒泡排序(Bubble Sort)
使用Jedis監聽Redis Stream 實現消息隊列功能