当前位置:网站首页>leetcode---每日一题
leetcode---每日一题
2022-07-17 00:14:00 【jjj34】
题目描述

思路:
通过题目我们可以看到以下限制条件
速度 k的 范围为 所有香蕉的和 / h <= k <= max(piles)
由于所有香蕉的和超出了int 范围,因此
优化后的范围为 0 <= k <= max(piles)
思路1.既然在一定的范围之内,我们就可以尝试暴力破解
从0开始,每一次都 k+=1 ,直到能够将香蕉吃完
思路2.当数据太大的时候,超出了时间范围,因此可以采用二分法进行查找
拿到一个数,这个速度能够吃完,right = k;不能吃完,left = k+1;k=left+(right-left)/2;
当然了,我们还得写一个函数判断能不能吃完
代码如下
class Solution {
public int minEatingSpeed(int[] piles, int h) {
int left = 1;
int right = 0;
for(int temp : piles)
{
right = Math.max(right,temp);
}
int k=0;
while(left<right)
{
k = left+(right - left)/2;
//写一个函数判断能不能吃完,并返回boolean
if(judge(piles,k,h))
{
//能吃完
right = k;
}
else
{
left = k+1;
}
}
return right;
}
public boolean judge(int []piles,int b,int h)
{
int count1=0;
for(int temp : piles)
{
int temp2 = (temp + b -1)/b;
count1 += temp2;
}
if(count1 > h)//吃不完
{
return false;
}
else
{
return true;
}
}
}遇到的问题
1.for循环知识点的引用
for(int temp : piles)
{}
代替
for(int i=0;i<piles.length;i++)
{
int temp = piles[i];
}
2.计算每一堆吃的次数
while(temp > 0)
{
temp -= speed;
count1 ++;
}
替换为
temp = (temp-1 + speed ) /speed;
count1 += temp;
// temp-1 + speed 是为了保证 堆数小于速度时,保证次数为 1
//直接用 temp/speed 可能会出现吃了,但吃的次数为 0边栏推荐
- 项目性能优化实战:解决首页白屏问题,自定义 loading 动画优化首屏效果
- 【Unity开发小技巧】Unity混音器Mixer控制全局音量
- gdb+vscode进行调试7——程序出现segmentation default/段错误,如何进行调试?
- STL -- string container
- 简述特征工程及其sklearn的实现
- 英文商务邮件常用语
- 工程编译那点事:Makefile和cmake(一)
- [tools] unity screen drawing line, unity screen drawing Hsj drawing tool
- [unity development tips] unity packs the EXE on the PC side and compresses and packs it into an EXE file
- [unity panel attribute literacy] set texture import settings after importing textures
猜你喜欢

ENVI_IDL:讀取所有OMI產品的NO2柱含量並計算月均值、季均值、年均值+解析

gdb+vscode进行调试0——环境配置
![[unity development tips] unity packs the EXE on the PC side and compresses and packs it into an EXE file](/img/b1/6ca7164d218c179918bb2497ab5fa3.png)
[unity development tips] unity packs the EXE on the PC side and compresses and packs it into an EXE file

bugku---game1

Envi IDL: lire la teneur en colonne de NO2 de tous les produits OMI et calculer la moyenne mensuelle, la moyenne trimestrielle, la moyenne annuelle + résolution

攻防世界----shrine

元宇宙公链Caduceus项目详解(一):Caduceus Metaverse Protocol的项目理念及技术框架

ENVI_IDL:批量对Modis Swath产品进行均值运算+解析

Dueling DQN的理论基础及其代码实现【Pytorch + Pendulum-v0】

Swagger——世界上最流行的Api框架
随机推荐
Envi IDL: lire la teneur en colonne de NO2 de tous les produits OMI et calculer la moyenne mensuelle, la moyenne trimestrielle, la moyenne annuelle + résolution
【工具篇】SQLite本地数据库在Unity3D的应用
Dueling DQN的理论基础及其代码实现【Pytorch + Pendulum-v0】
gdb+vscode进行调试7——程序出现segmentation default/段错误,如何进行调试?
字符串转换为整数
散列表、布隆过滤器、分布式一致性hash
Leetcode 1: Two Sum
Gdb+vscode for debugging 0 - environment configuration
【工具篇】Unity快速上手制作2D和2.5D游戏的神器TileMap
字符串全排列问题
SSTI模板注入
[unity Editor Extension] scriptableobject for internal asset configuration of unity
新手如何配置多个 SSH Key(通俗易懂手把手教学)
工程编译那点事:Makefile和cmake(一)
Vmware Tools最新安装教程(RHEL8)
成信大ENVI_IDL第二周实验内容:提取所有MODIS气溶胶产品中AOD+详细解析
【Unity编辑器扩展】Unity资产预处理和后处理图片自动转Sprite2D
ENVI_IDL: 读取文本文件并输出为Geotiff格式+简单均值插值
[dynamic planning hundred questions strengthening plan] 1~10 (continuously updating)
gdb+vscode进行调试6——gdb调试多线程命令札记