当前位置:网站首页>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边栏推荐
- leetcode力扣经典问题——42.接雨水
- Configure map reduce workflow in oozie
- Line process of pool components
- 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
- 【Unity编辑器扩展】快速定位资源和脚本的指定文件和路径
- Gdb+vscode for debugging 7 - how to debug when there is a segmentation default/ segment error in the program?
- ENVI_IDL: 读取文本文件并输出为Geotiff格式+简单均值插值
- 信号与系统实验
- Leetcode 322: Coin Change - 动态规划
猜你喜欢

攻防世界----easytornado笔记

STL -- stack container

ENVI_IDL: 读取文本文件并输出为Geotiff格式+简单均值插值

ENVI_ Idl: read the NO2 column content of all OMI products and calculate the monthly average, quarterly average, annual average + analysis

Double Q-Learning理论基础及其代码实现【Pendulum-v0】

3D NFT的破茧重生:Caduceus去中心化边缘渲染技术

SSTI模板注入

池式组件之内存池篇

项目性能优化实战:解决首页白屏问题,自定义 loading 动画优化首屏效果

Gdb+vscode for debugging 0 - environment configuration
随机推荐
(附word操作以及视频讲解)使用ARCGIS进行地图配准_投影变换_普通地图制作_专题地图制作
字符串全排列问题
去中心化边缘渲染元宇宙协议Caduceus受邀出席CBAIA 2022峰会,以技术赋能更多Web3应用场景
【AntV G2】如何解决 G2 造成的内存泄露
第2章-系统控制原理 -> 经典控制理论
图像质量评估指标:SNR、PSNR、MSE和SSIM
信号与系统实验
【Unity开发小技巧】Unity混音器Mixer控制全局音量
JS笔记1
Leetcode 198:House Robber
bugku题解
《Visual C#从入门到精通》个人学习整理
[hdrp HD rendering pipeline] create hdrp project and upgrade the built-in pipeline project to hdrp project
LeetCode:动态规划中的0-1背包问题【快来直接套模板啦】
Line process of pool components
Hue oozie editor scheduling shell
Buaaos-lab0 experimental report
gdb+vscode进行调试2——gdb断点相关
[dynamic planning hundred questions strengthening plan] 1~10 (continuously updating)
gdb+vscode进行调试3——vscode以及gdb远程调试