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

组合模式、透明方式和安全方式

LeetCode_ Binary search tree_ Simple_ 108. convert an ordered array to a binary search tree
Posting - don't get lost in the ocean of Technology

【 langage c】 stockage des données d'analyse approfondie en mémoire
![[arm] add desktop application for buildreoot of rk3568 development board](/img/9a/28015cdea7362261c39ffc7f6e13a9.png)
[arm] add desktop application for buildreoot of rk3568 development board

MySQL数据库-01数据库概述

String类学习

When was the autowiredannotationbeanpostprocessor instantiated?

Ribbon负载均衡服务调用

Bingc (inheritance)
随机推荐
Talk 5 wireless communication
[PHP] PHP two-dimensional array is sorted by multiple fields
DOM document
RIA想法
使用Jenkins执行TestNg+Selenium+Jsoup自动化测试和生成ExtentReport测试报告
[MySQL] MySQL million level data paging query method and its optimization
Project suspension
使用Jedis监听Redis Stream 实现消息队列功能
Introduction to alluxio
国务院发文,完善身份认证、电子印章等应用,加强数字政府建设
Pytorch (environment, tensorboard, transforms, torchvision, dataloader)
Daily production training report (15)
pytorch(环境、tensorboard、transforms、torchvision、dataloader)
redis探索之布隆过滤器
ZigBee learning in simple terms Lecture 1
Henkel database custom operator '~~‘
定位设置水平,垂直居中(多种方法)
【C语言】深度剖析数据在内存中的存储
Pytorch (network model training)
新的征程