当前位置:网站首页>[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[][]{
}));
}
}
边栏推荐
- Cordova plugin device obtains the device information plug-in, which causes Huawei to fail the audit
- Confluence的PDF导出中文文档异常显示问题解决
- Graduation summary
- Shell脚本基本使用
- [jetcache] jetcache configuration description and annotation attribute description
- Centos7 one click compilation to build MySQL script
- 1.12 - Instructions
- v8
- JSON转换工具类
- node_ Modules cannot be deleted
猜你喜欢

1.12 - Instructions

Rust string slicing, structs, and enumeration classes

Shell脚本基本使用

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

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

Multiprocess programming (I): basic concepts

antv x6节点拖拽到画布上后的回调事件(踩大坑记录)

Hdu3507 (slope DP entry)

AEM: Nanlin fan Ben et al. - plant rhizosphere growth promoting bacteria control soybean blight

DotNet圈里一个优秀的ORM——FreeSql
随机推荐
Free we media essential tools sharing
Sentry developer contribution Guide - configure pycharm
MySQL 23道经典面试吊打面试官
The "2022 China Digital Office Market Research Report" can be downloaded to explain the 176.8 billion yuan market in detail
2022上半年值得被看见的10条文案,每一句都能带给你力量!
Problèmes de configuration lex & yacc & Bison & Flex
Leetcode 294. Flip game II (game theory)
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)
NC24325 [USACO 2012 Mar S]Flowerpot
Array common operation methods sorting (including ES6) and detailed use
Hundreds of continuous innovation to create free low code office tools
【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(中)-- 搜索建议
毕业总结
简单聊聊运维监控的其他用途
【AutoSAR 五 方法论】
Form form instantiation
【JetCache】JetCache的配置说明和注解属性说明
AEM: Nanlin fan Ben et al. - plant rhizosphere growth promoting bacteria control soybean blight
NC17059 队列Q
Vulkan-实践第一弹