当前位置:网站首页>力扣 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;
}
}
边栏推荐
- Customize WebService as a proxy to solve the problem of Silverlight calling WebService across domains
- cartographer_ pose_ graph_ 2d
- pytorch(环境、tensorboard、transforms、torchvision、dataloader)
- Experience of reading the road to wealth and freedom
- 一段不离不弃的爱情
- vscode config
- 无线网络存在的安全问题及现代化解决方案
- 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 (,,,,,)
- Installation and deployment of alluxio
- Supplementary course on basic knowledge of IM development (II): how to design a server-side storage architecture for a large number of image files?
猜你喜欢

pytorch(网络模型训练)

PHP 2D / multidimensional arrays are sorted in ascending and descending order according to the specified key values

How to rewrite a pseudo static URL created by zenpart

cartographer_optimization_problem_2d
![[activity recommendation] cloud native, industrial Internet, low code, Web3, metauniverse... Which is the architecture hot spot in 2022](/img/64/5b2aec7a26c64c104c86e200f83b2d.png)
[activity recommendation] cloud native, industrial Internet, low code, Web3, metauniverse... Which is the architecture hot spot in 2022

Mongodb image configuration method

cartographer_local_trajectory_builder_2d

There are applications related to web network request API in MATLAB (under update)

cartographer_ local_ trajectory_ builder_ 2d

Leetcode114. 二叉树展开为链表
随机推荐
cartographer_ optimization_ problem_ 2d
2021年OWASP-TOP10
电机专用MCU芯片LCM32F037系列内容介绍
LeetCode_二叉搜索树_简单_108.将有序数组转换为二叉搜索树
1212312321
Could not get unknown property ‘*‘ for SigningConfig container of type org.gradle.api.internal
[PHP] PHP two-dimensional array is sorted by multiple fields
[red team] what preparations should be made to join the red team?
最后一次飞翔
What management systems (Updates) for things like this
数据存储:MySQL之InnoDB与MyISAM的区别
SDN based DDoS attack mitigation
Use jedis to monitor redis stream to realize message queue function
ZigBee explain in simple terms lesson 2 hardware related and IO operation
新的征程
11 IO frame
国务院发文,完善身份认证、电子印章等应用,加强数字政府建设
Supplementary course on basic knowledge of IM development (II): how to design a server-side storage architecture for a large number of image files?
The localstorage browser stores locally to limit the number of forms submitted when tourists do not log in.
DOM文档