当前位置:网站首页>【日常训练】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[][]{
}));
}
}
边栏推荐
猜你喜欢

可下载《2022年中国数字化办公市场研究报告》详解1768亿元市场

Automated defect analysis in electron microscopic images-论文阅读笔记

Callback event after the antv X6 node is dragged onto the canvas (stepping on a big hole record)

Feature Engineering: summary of common feature transformation methods
![[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)](/img/73/19e2e0fc5ea6f05e34584ba40a452d.jpg)
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)

Markdown使用教程

Multiprocess programming (II): Pipeline

How SQLSEVER removes data with duplicate IDS

Shell 实现文件基本操作(切割、排序、去重)

kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)
随机推荐
Overlay of shutter (Pop-Up)
Feature Engineering: summary of common feature transformation methods
pod生命周期详解
NC24325 [USACO 2012 Mar S]Flowerpot
Linux软件:如何安装Redis服务
Andorid gets the system title bar height
There is an unknown problem in inserting data into the database
Rust字符串切片、结构体和枚举类
UART、RS232、RS485、I2C和SPI的介绍
[shutter] Introduction to the official example of shutter Gallery (project introduction | engineering construction)
Introduction of UART, RS232, RS485, I2C and SPI
Shell脚本基本使用
Pytorch 20 realizes corrosion expansion based on pytorch
Basic use of shell script
【单片机项目实训】八路抢答器
The most painful programming problem in 2021, adventure of code 2021 Day24
Cmake basic use
How SQLSEVER removes data with duplicate IDS
Centos7 one click compilation to build MySQL script
Graduation summary