当前位置:网站首页>力扣 875. 爱吃香蕉的珂珂
力扣 875. 爱吃香蕉的珂珂
2022-06-26 05:36:00 【SP FA】
这是一道披着二分外衣的数学题。通过这道题学会了一个关于精度方面的小技巧:
由题意可知,我们如果想算吃 p i l e s [ i ] piles[i] piles[i] 所用的时间,可以通过 ⌈ p i l e s [ i ] m i d ⌉ \lceil\frac{piles[i]}{mid}\rceil ⌈midpiles[i]⌉ 来计算,其中 m i d mid mid 是二分的时候的中值。但这样在数字很大时会涉及到精度的问题,如 p i l e s [ i ] = 1000000000 piles[i]=1000000000 piles[i]=1000000000, m i d = 999999986 mid=999999986 mid=999999986,此时使用上面的公式算出结果是 2,但实际上应该是 3 才对。
解决方法
由于 p i l e s [ i ] > 0 piles[i]>0 piles[i]>0, m i d > 0 mid>0 mid>0,于是原式子等价于 ⌊ 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⌋,这相当于在不影响结果的情况下对 p i l e s [ i ] piles[i] piles[i] 进行了放大,数变大了结果就会变大,于是避免了精度问题。具体可以将上数代入感受一下。
关于二分的思路很好想,不多赘述。直接上代码:
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;
}
}
边栏推荐
- SDN based DDoS attack mitigation
- Fedora alicloud source
- 循环位移
- Mongodb image configuration method
- Baidu API map is not displayed in the middle, but in the upper left corner. What's the matter? Resolved!
- The parameter field of the callback address of the payment interface is "notify_url", and an error occurs after encoding and decoding the signed special character URL (,,,,,)
- The difference between get and post in small interview questions
- 自定义WebSerivce作为代理解决SilverLight跨域调用WebService问题
- Cyclic displacement
- SOFA Weekly | 开源人—于雨、本周 QA、本周 Contributor
猜你喜欢
Red team scoring method statistics
Baidu API map is not displayed in the middle, but in the upper left corner. What's the matter? Resolved!
Yunqi lab recommends experience scenarios this week, free cloud learning
【ARM】讯为rk3568开发板buildroot添加桌面应用
Official image acceleration
SDN based DDoS attack mitigation
Could not get unknown property ‘*‘ for SigningConfig container of type org. gradle. api. internal
The wechat team disclosed that the wechat interface is stuck with a super bug "15..." The context of
How Navicat reuses the current connection information to another computer
pytorch(网络模型)
随机推荐
ZigBee learning in simple terms Lecture 1
基于SDN的DDoS攻击缓解
Last flight
June 3 is a happy day
一段不离不弃的爱情
cartographer_ optimization_ problem_ 2d
uni-app吸顶固定样式
A new explanation of tcp/ip five layer protocol model
Leetcode513. Find the value in the lower left corner of the tree
CMakeLists.txt Template
Replacing domestic image sources in openwrt for soft routing (take Alibaba cloud as an example)
The localstorage browser stores locally to limit the number of forms submitted when tourists do not log in.
MySQL source code reading (II) login connection debugging
REUSE_ALV_GRID_DISPLAY 事件实现(DATA_CHANGED)
SOFA Weekly | 开源人—于雨、本周 QA、本周 Contributor
data = self._ data_ queue. get(timeout=timeout)
cartographer_ pose_ graph_ 2d
Summary of the 10th provincial Blue Bridge Cup
Data storage: the difference between MySQL InnoDB and MyISAM
旧情书