当前位置:网站首页>【日常训练】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[][]{
}));
}
}
边栏推荐
- Some introduction and precautions about XML
- 奥斯陆大学:Li Meng | 基于Swin-Transformer的深度强化学习
- [MCU project training] eight way answering machine
- How SQLSEVER removes data with duplicate IDS
- NC50965 Largest Rectangle in a Histogram
- [IELTS reading] Wang Xiwei reading P1 (reading judgment question)
- Redis21 classic interview questions, extreme pull interviewer
- Bigder: how to deal with the bugs found in the 32/100 test if they are not bugs
- Markdown tutorial
- [shutter] Introduction to the official example of shutter Gallery (project introduction | engineering construction)
猜你喜欢

TypeError: Cannot read properties of undefined (reading ***)

UART、RS232、RS485、I2C和SPI的介绍

Confluence的PDF导出中文文档异常显示问题解决

Hundreds of continuous innovation to create free low code office tools

Linux软件:如何安装Redis服务
![[MCU project training] eight way answering machine](/img/a3/6a50619cd16269bf485a4a273677aa.jpg)
[MCU project training] eight way answering machine

What website can you find English literature on?

University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement

2022上半年值得被看见的10条文案,每一句都能带给你力量!
![[shutter] Introduction to the official example of shutter Gallery (project introduction | engineering construction)](/img/f7/a8eb8e40b9ea25021751d7150936ac.jpg)
[shutter] Introduction to the official example of shutter Gallery (project introduction | engineering construction)
随机推荐
Linux Software: how to install redis service
关于QByteArray存储十六进制 与十六进制互转
【单片机项目实训】八路抢答器
字符设备注册常用的两种方法和步骤
Introduction of UART, RS232, RS485, I2C and SPI
简单聊聊运维监控的其他用途
Pytorch 20 realizes corrosion expansion based on pytorch
使用jenkins之二Job
setInterval定时器在ie不生效原因之一:回调的是箭头函数
[IELTS reading] Wang Xiwei reading P1 (reading judgment question)
NC24325 [USACO 2012 Mar S]Flowerpot
程序分析与优化 - 9 附录 XLA的缓冲区指派
Should you study kubernetes?
Shell implements basic file operations (SED edit, awk match)
Hundreds of continuous innovation to create free low code office tools
Confluence的PDF导出中文文档异常显示问题解决
TypeError: Cannot read properties of undefined (reading ***)
pod生命周期详解
多进程编程(一):基本概念
Why is the website slow to open?