当前位置:网站首页>[daily training] 871 Minimum refueling times
[daily training] 871 Minimum refueling times
2022-07-03 00:42:00 【Puppet__】
subject
The car set off from the starting point to the destination , The purpose is to the east of the starting position target Miles away .
There are gas stations along the way , Every station[i] For a gas station , It's east of the starting point station[i][0] Miles away , And there are station[i][1] A litre of petrol .
Suppose the capacity of the car's fuel tank is infinite , At first there was startFuel Liter fuel . Every time it drives 1 Miles will be used 1 A litre of petrol .
When the car arrives at the gas station , It may stop to refuel , Transfer all gasoline from the gas station to the car .
In order to reach the destination , What's the minimum number of refuels necessary for a car ? If you can't get to your destination , Then return to -1 .
Be careful : If the remaining fuel when the car arrives at the gas station is 0, It can still refuel there . If the remaining fuel is 0, Still think it has reached its destination .
Example 1:
Input :target = 1, startFuel = 1, stations = []
Output :0
explain : We can get to our destination without refueling .
Example 2:
Input :target = 100, startFuel = 1, stations = [[10,100]]
Output :-1
explain : We can't get to our destination , Not even getting to the first gas station .
Example 3:
Input :target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]]
Output :2
explain :
We set out with 10 Liter fuel .
We drove to the starting point 10 The gas station at mile , Consume 10 Liter fuel . Take the gas from 0 To add to 60 l .
then , We from 10 The gas station is miles away 60 The gas station at mile ( Consume 50 Liter fuel ),
And take the gas from 10 To add to 50 l . Then we drove to our destination .
We are on the way 1 Two gas stations stop , So back 2 .
Tips :
1 <= target, startFuel, stations[i][1] <= 109
0 <= stations.length <= 500
0 < stations[0][0] < stations[1][0] < … < stations[stations.length-1][0] < target
Code
package dayLeetCode;
public class dayleetcoe871 {
// dp dp[i] It means refueling i The maximum number of miles traveled
// Take the oil away , Whether you use it or not depends on the specific situation , Always get the biggest oil
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++){
// Before judgment j Can I get there with this refueling i, If it can be reached, it will be judged whether it will be i Fill the car with gas from a gas station
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]);
}
}
}
// Find the least number of refuelling
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[][]{
}));
}
}
边栏推荐
- Nc20806 District interval
- Extension of flutter
- Shell implements basic file operations (SED edit, awk match)
- JSON conversion tool class
- [IELTS reading] Wang Xiwei reading P1 (reading judgment question)
- Pageoffice - bug modification journey
- 奥斯陆大学:Li Meng | 基于Swin-Transformer的深度强化学习
- 程序分析与优化 - 9 附录 XLA的缓冲区指派
- 【AutoSAR 十一 通信相关机制】
- FAQ | FAQ for building applications for large screen devices
猜你喜欢

Feature Engineering: summary of common feature transformation methods

Win10 多种方式解决无法安装.Net3.5的问题

【AutoSAR 七 工具链简介】

利亚德:Micro LED 产品消费端首先针对 100 英寸以上电视,现阶段进入更小尺寸还有难度

Vulkan-性能及精细化

Multiprocess programming (I): basic concepts

An excellent orm in dotnet circle -- FreeSQL

Use Jenkins II job

University of Toronto:Anthony Coache | 深度强化学习的条件可诱导动态风险度量

Is there a free text to speech tool to help recommend?
随机推荐
Logback configuration file
多进程编程(三):消息队列
Shell脚本基本使用
Leetcode 294. Flip game II (game theory)
UART、RS232、RS485、I2C和SPI的介绍
多进程编程(五):信号量
Two common methods and steps of character device registration
Nacos+openfeign error reporting solution
Briefly talk about other uses of operation and maintenance monitoring
Set up nacos2 X cluster steps and problems encountered
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)
使用jenkins之二Job
Multiprocess programming (V): semaphores
Rust string slicing, structs, and enumeration classes
【日常训练】871. 最低加油次数
Explain in detail the significance of the contour topology matrix obtained by using the contour detection function findcontours() of OpenCV, and how to draw the contour topology map with the contour t
[Luogu p4320] road meets (round square tree)
多进程编程(二):管道
How to find out the currently running version of Solr- How do I find out version of currently running Solr?
1.12 - Instructions