当前位置:网站首页>871. Minimum Number of Refueling Stops
871. Minimum Number of Refueling Stops
2022-07-05 11:27:00 【soO_ 007】
subject :
A car travels from a starting position to a destination which is target miles east of the starting position.
There are gas stations along the way. The gas stations are represented as an array stations where stations[i] = [positioni, fueli] indicates that the ith gas station is positioni miles east of the starting position and has fueli liters of gas.
The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it. It uses one liter of gas per one mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car.
Return the minimum number of refueling stops the car must make in order to reach its destination. If it cannot reach the destination, return -1.
Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there. If the car reaches the destination with 0 fuel left, it is still considered to have arrived.
Example 1:
Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target without refueling.
Example 2:
Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can not reach the target (or even the first gas station).
Example 3:
Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2 Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60 (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. We made 2 refueling stops along the way, so we return 2.
Constraints:
1 <= target, startFuel <= 1090 <= stations.length <= 5000 <= positioni <= positioni+1 < target1 <= fueli < 109
Ideas 1:
The first reaction is to do it with recursion ,f[i][j] It means that after the second i After a gas station , come on. j The maximum distance that can be reached , Finally, you only need to find the smallest j also f[i][j] Greater than or equal to target that will do . In the state transition equation f[i][j] You can come from three places :1)f[i][j - 1], The representative passed the current station , But don't refuel at the current station ;2)f[i - 1][j], It means reaching the i - 1 That is, the previous station , And added j Secondary oil , That is, there was no refueling at the last station ;3)f[i - 1][j - 1] + stations[i - 1][1], That is, after the i - 1 A station , And added oil at the last station , Of course, the condition here is f[i - 1][j - 1] The maximum distance of must be greater than stations[i - 1][0] Big , Otherwise, you can't get to the last station . Initialization we use n = stations.size() + 1, namely f[i][j] When j be equal to 0 It means f[i][0] All of them have not been oiled , Then their value should be startFuel, as for f[0][j], No initialization required , because j It must be less than waiting i, That is, you can't refuel more than the passing station .
Code 1:
class Solution {
public:
int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
int n = stations.size() + 1;
vector<vector<long>> f(n, vector<long>(n));
for (int i = 0; i < n; i++) {
f[i][0] = startFuel;
}
for (int i = 1; i < n; i++) {
for (int j = 1; j <= i; j++) {
if (f[i - 1][j - 1] >= stations[i - 1][0]) {
f[i][j] = f[i - 1][j - 1] + stations[i - 1][1];
}
f[i][j] = max({f[i - 1][j], f[i][j - 1], f[i][j]});
}
}
for (int j = 0; j < n; j++) {
for (int i = 0; i < n; i++) {
if (f[i][j] >= target)
return j;
}
}
return -1;
}
};
Ideas 2:
In essence , The problem requires that we need to add the most oil in the minimum number of refueling , That is, add the most oil you can every time you refuel . But every time we pass the station , In fact, only the current station can choose , So how to form options ? The answer is not to refuel every time you pass the station , Record the fuel that can be added into a priority queue , When we need to refuel, we can take it out and refuel for ourselves . The priority queue here is to manage the maximum , What we can take out at any time is the most oil . First of all, the maximum distance we can reach dist Namely startFuel, After judgment dist and target, once dist Greater than or equal to target It's over . In circulation , Every time we pass the station , If at present dist Be able to reach the station , Then let's not refuel , Instead, add oil to the priority queue ; If you can't reach the station , Then come on , If the priority queue has run out of oil , Prove that we can't reach target, Go straight back to -1; If the priority queue has oil , Then take out the biggest oil , give dist, At the same time, update the refueling times .
Code 2:
class Solution {
public:
int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
priority_queue<int> q;
int dist = startFuel, count = 0;
int i = 0;
while (dist < target) {
if (i < stations.size() && dist >= stations[i][0]) { // Being able to get to the station means that the oil is enough for the time being
q.push(stations[i][1]); // Don't refuel first , Save up
i++;
} else {
if (q.size()) { // There is oil to add
dist += q.top(); // come on.
q.pop();
count++; // Number of updates
} else { // No oil to add , Cannot reach target
return -1;
}
}
}
return count;
}
};
边栏推荐
- MFC pet store information management system
- FreeRTOS 中 RISC-V-Qemu-virt_GCC 的调度时机
- Golang application topic - channel
- 【DNS】“Can‘t resolve host“ as non-root user, but works fine as root
- Sklearn model sorting
- [JS learning notes 54] BFC mode
- AutoCAD -- mask command, how to use CAD to locally enlarge drawings
- 2022 t elevator repair operation certificate examination questions and answers
- Basic part - basic project analysis
- ZCMU--1390: 队列问题(1)
猜你喜欢

Huawei equipment configures channel switching services without interruption

技术管理进阶——什么是管理者之体力、脑力、心力

Question bank and answers of special operation certificate examination for main principals of hazardous chemical business units in 2022

紫光展锐全球首个5G R17 IoT NTN卫星物联网上星实测完成

COMSOL -- establishment of 3D graphics

Advanced technology management - what is the physical, mental and mental strength of managers

【广告系统】增量训练 & 特征准入/特征淘汰

Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment

Three suggestions for purchasing small spacing LED display

Stop saying that microservices can solve all problems!
随机推荐
爬虫(9) - Scrapy框架(1) | Scrapy 异步网络爬虫框架
Lombok makes ⽤ @data and @builder's pit at the same time. Are you hit?
R3live series learning (IV) r2live source code reading (2)
解决grpc连接问题Dial成功状态为TransientFailure
[TCP] TCP connection status JSON output on the server
CDGA|数据治理不得不坚持的六个原则
Spark Tuning (I): from HQL to code
Cron表达式(七子表达式)
Ddrx addressing principle
使用GBase 8c数据库过程中报错:80000305,Host ips belong to different cluster ,怎么解决?
2022 Pengcheng cup Web
Oneforall installation and use
shell脚本文件遍历 str转数组 字符串拼接
基于OpenHarmony的智能金属探测器
项目总结笔记系列 wsTax KT Session2 代码分析
Technology sharing | common interface protocol analysis
Codeforces Round #804 (Div. 2)
FFmpeg调用avformat_open_input时返回错误 -22(Invalid argument)
阻止浏览器后退操作
基于Lucene3.5.0怎样从TokenStream获得Token