当前位置:网站首页>力扣 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;
}
}
边栏推荐
- 新的征程
- 项目中止
- The news of thunderbolt
- A new journey
- Mise en file d'attente des messages en utilisant jedis Listening redis stream
- Install the tp6.0 framework under windows, picture and text. Thinkphp6.0 installation tutorial
- Learn cache lines and pseudo sharing of JVM slowly
- 11 IO frame
- [MySQL] MySQL million level data paging query method and its optimization
- About abstact and virtual
猜你喜欢

Wechat team sharing: technical decryption behind wechat's 100 million daily real-time audio and video chats

LeetCode_二叉搜索树_简单_108.将有序数组转换为二叉搜索树
![[upsampling method opencv interpolation]](/img/6b/5e8f3c2844f0cbbbf03022e0efd5f0.png)
[upsampling method opencv interpolation]

Fedora alicloud source

操作符的优先级、结合性、是否控制求值顺序【详解】

cartographer_fast_correlative_scan_matcher_2d分支定界粗匹配

How does P2P technology reduce the bandwidth of live video by 75%?

Navicat如何将当前连接信息复用另一台电脑
![[arm] add desktop application for buildreoot of rk3568 development board](/img/9a/28015cdea7362261c39ffc7f6e13a9.png)
[arm] add desktop application for buildreoot of rk3568 development board

Ribbon负载均衡服务调用
随机推荐
Mongodb image configuration method
Using Jenkins to perform testng+selenium+jsup automated tests and generate extendreport test reports
LeetCode_ Binary search tree_ Simple_ 108. convert an ordered array to a binary search tree
DOM文档
FindControl的源代码
C XX management system
Apktool tool usage document
[red team] what preparations should be made to join the red team?
MySQL source code reading (II) login connection debugging
慢慢学JVM之缓存行和伪共享
Old love letters
Baidu API map is not displayed in the middle, but in the upper left corner. What's the matter? Resolved!
最后一次飞翔
新的征程
SDN based DDoS attack mitigation
vscode config
Secondary bootloader about boot28 Precautions for ASM application, 28035
第九章 设置结构化日志记录(一)
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 (,,,,,)
cartographer_optimization_problem_2d