当前位置:网站首页>力扣 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;
}
}
边栏推荐
- Daily production training report (15)
- cartographer_backend_constraint
- 虚拟项目失败感想
- MySQL数据库-01数据库概述
- Official image acceleration
- RIA ideas
- How Navicat reuses the current connection information to another computer
- How to ensure the efficiency and real-time of pushing large-scale group messages in mobile IM?
- 旧情书
- 新的征程
猜你喜欢

The localstorage browser stores locally to limit the number of forms submitted when tourists do not log in.

Recursively traverse directory structure and tree presentation

Mongodb image configuration method

慢慢学JVM之缓存行和伪共享

cartographer_pose_graph_2d

Serious hazard warning! Log4j execution vulnerability is exposed!

MySQL数据库-01数据库概述

LeetCode_ Binary search tree_ Simple_ 108. convert an ordered array to a binary search tree

pytorch(网络模型训练)

Wechat team sharing: technical decryption behind wechat's 100 million daily real-time audio and video chats
随机推荐
The news of thunderbolt
The wechat team disclosed that the wechat interface is stuck with a super bug "15..." The context of
Yunqi lab recommends experience scenarios this week, free cloud learning
How to rewrite a pseudo static URL created by zenpart
Some doubts about ARP deception experiment
Replacing domestic image sources in openwrt for soft routing (take Alibaba cloud as an example)
Official image acceleration
About abstact and virtual
Cyclic displacement
cross entropy loss = log softmax + nll loss
How to make your big file upload stable and fast?
Could not get unknown property ‘*‘ for SigningConfig container of type org.gradle.api.internal
As promised: Mars, the mobile terminal IM network layer cross platform component library used by wechat, has been officially open source
Something about MariaDB
Installation and deployment of alluxio
Internship May 29, 2019
Baidu API map is not displayed in the middle, but in the upper left corner. What's the matter? Resolved!
Introduction to GUI programming to game practice (I)
Henkel database custom operator '~~‘
C# 39. Conversion between string type and byte[] type (actual measurement)