当前位置:网站首页>【日常训练】871. 最低加油次数
【日常训练】871. 最低加油次数
2022-07-02 23:43:00 【Puppet__】
题目
汽车从起点出发驶向目的地,该目的地位于出发位置东面 target 英里处。
沿途有加油站,每个 station[i] 代表一个加油站,它位于出发位置东面 station[i][0] 英里处,并且有 station[i][1] 升汽油。
假设汽车油箱的容量是无限的,其中最初有 startFuel 升燃料。它每行驶 1 英里就会用掉 1 升汽油。
当汽车到达加油站时,它可能停下来加油,将所有汽油从加油站转移到汽车中。
为了到达目的地,汽车所必要的最低加油次数是多少?如果无法到达目的地,则返回 -1 。
注意:如果汽车到达加油站时剩余燃料为 0,它仍然可以在那里加油。如果汽车到达目的地时剩余燃料为 0,仍然认为它已经到达目的地。
示例 1:
输入:target = 1, startFuel = 1, stations = []
输出:0
解释:我们可以在不加油的情况下到达目的地。
示例 2:
输入:target = 100, startFuel = 1, stations = [[10,100]]
输出:-1
解释:我们无法抵达目的地,甚至无法到达第一个加油站。
示例 3:
输入:target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]]
输出:2
解释:
我们出发时有 10 升燃料。
我们开车来到距起点 10 英里处的加油站,消耗 10 升燃料。将汽油从 0 升加到 60 升。
然后,我们从 10 英里处的加油站开到 60 英里处的加油站(消耗 50 升燃料),
并将汽油从 10 升加到 50 升。然后我们开车抵达目的地。
我们沿途在1两个加油站停靠,所以返回 2 。
提示:
1 <= target, startFuel, stations[i][1] <= 109
0 <= stations.length <= 500
0 < stations[0][0] < stations[1][0] < … < stations[stations.length-1][0] < target
代码
package dayLeetCode;
public class dayleetcoe871 {
// dp dp[i]表示加油i次的最大行驶英里数
// 油要带走,用不用的话需要看具体情况,永远拿最大的油
public int minRefuelStops(int target, int startFuel, int[][] stations) {
long[] dp = new long[stations.length + 1];
dp[0] = startFuel;
for (int i = 0; i < stations.length; i++){
// 判断前j次加油能不能到达i, 能到达的话则判断将不将第i个加油站的油加到车里
for (int j = i; j >= 0; j--){
if (dp[j] >= stations[i][0]){
dp[j + 1] = Math.max(dp[j + 1], dp[j] + stations[i][1]);
}
}
}
// 寻找最少的加油次数
for (int i = 0; i <= stations.length; i++){
if (dp[i] >= target){
return i;
}
}
return -1;
}
public static void main(String[] args) {
dayleetcoe871 obj = new dayleetcoe871();
System.out.println(obj.minRefuelStops(1, 1, new int[][]{
}));
}
}
边栏推荐
- 多进程编程(五):信号量
- Multiprocess programming (II): Pipeline
- Understanding and application of least square method
- An excellent orm in dotnet circle -- FreeSQL
- FAQ | FAQ for building applications for large screen devices
- Monitor container runtime tool Falco
- [IELTS reading] Wang Xiwei reading P2 (reading fill in the blank)
- Linux Software: how to install redis service
- Bigder: how to deal with the bugs found in the 32/100 test if they are not bugs
- 百数不断创新,打造自由的低代码办公工具
猜你喜欢

Automated defect analysis in electronic microscopic images

Bloom filter

setInterval定时器在ie不生效原因之一:回调的是箭头函数

Logback configuration file

Shell 实现文件基本操作(sed-编辑、awk-匹配)

Introduction of UART, RS232, RS485, I2C and SPI

FAQ | FAQ for building applications for large screen devices

pageoffice-之bug修改之旅

The "2022 China Digital Office Market Research Report" can be downloaded to explain the 176.8 billion yuan market in detail

Shell implements basic file operations (cutting, sorting, and de duplication)
随机推荐
TypeError: Cannot read properties of undefined (reading ***)
The most painful programming problem in 2021, adventure of code 2021 Day24
Shell implements basic file operations (cutting, sorting, and de duplication)
NC50965 Largest Rectangle in a Histogram
What is the standard format of a 2000-3000 word essay for college students' classroom homework?
Shell implements basic file operations (SED edit, awk match)
[golang syntax] map common errors golang panic: assignment to entry in nil map
Redis21 classic interview questions, extreme pull interviewer
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)
Why is the website slow to open?
University of Oslo: Li Meng | deep reinforcement learning based on swing transformer
Markdown tutorial
FRP reverse proxy +msf get shell
Luogu_ P2010 [noip2016 popularization group] reply date_ Half enumeration
Bigder:32/100 测试发现的bug开发认为不是bug怎么处理
Basic use of shell script
多进程编程(四):共享内存
MySQL 23 classic interview hanging interviewer
Where can I find foreign papers?
Free we media essential tools sharing