LC 871. Minimum Number of Refueling Stops

A car travels from a starting position to a destination which is target miles east of the starting position.

Along the way, there are gas stations.  Each station[i] represents a gas station that is station[i][0]miles east of the starting position, and has station[i][1] liters of gas.

The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it.  It uses 1 liter of gas per 1 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.

What is the least 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.

经典的小车加油问题。

【一定要注意看题,每个变量是什么意思。前几次都把stations[i][0]看成相邻两个加油站的距离了。。。】

利用优先队列。因为要求最小的加油次数,到了一个加油站不加油,但进入优先队列,不能到达终点或者下一个加油站的时候再取最大的。

 1 class Solution {
 2 public:
 3     int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
 4         priority_queue<int> pq;
 5         int driven = 0;
 6         int idx = 0;
 7         int ret = 0;
 8         while (startFuel < target) {
 9             if (idx >= stations.size()) {
10                 if(pq.empty()) return -1;
11                 while (!pq.empty() && startFuel < target) {
12                     startFuel += pq.top(); pq.pop();
13                     ret++;
14                 }
15                 if (startFuel >= target) return ret;
16                 else return -1;
17             }
18             if (startFuel >= stations[idx][0]) {
19                 pq.push(stations[idx][1]);
20             }
21             else {
22                 if (pq.empty()) return -1;
23                 while (!pq.empty() && startFuel < stations[idx][0]) {
24                     startFuel += pq.top(); pq.pop();
25                     ret++;
26                     if (startFuel >= target) return ret;
27                 }
28                 if(startFuel < stations[idx][0]) return -1;
29                 pq.push(stations[idx][1]);
30             }
31             idx++;
32         }
33         return ret;
34     }
35 };

另一个种解法:

 1     int minRefuelStops(int target, int cur, vector<vector<int>> s) {
 2         int i = 0, res;
 3         priority_queue<int>pq;
 4         for (res = 0; cur < target; res++) {
 5             while (i < s.size() && s[i][0] <= cur)
 6                 pq.push(s[i++][1]);
 7             if (pq.empty()) return -1;
 8             cur += pq.top(), pq.pop();
 9         }
10         return res;
11     }

原文地址:https://www.cnblogs.com/ethanhong/p/10136396.html

时间: 2024-07-30 06:01:20

LC 871. Minimum Number of Refueling Stops的相关文章

[LeetCode] 871. Minimum Number of Refueling Stops 最少的加油站个数

A car travels from a starting position to a destination which is?target?miles east of the starting position. Along the way, there are gas stations.? Each?station[i]?represents a gas station that is?station[i][0]?miles east of the starting position, a

[LC] 452. Minimum Number of Arrows to Burst Balloons

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of s

452. Minimum Number of Arrows to Burst Balloons扎气球的个数最少

[抄题]: There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinate

452. Minimum Number of Arrows to Burst Balloons——排序+贪心算法

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of s

Interval Minimum Number

Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. Each query has two integers [start, end]. For each query, calculate the minimum number between index start and end in the given array, return the resu

Reorder array to construct the minimum number

Construct minimum number by reordering a given non-negative integer array. Arrange them such that they form the minimum number. Notice The result may be very large, so you need to return a string instead of an integer. Have you met this question in a

codeforce 804B Minimum number of steps

cf劲啊 原题: We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each step we choose one of substrings "ab" in the string and replace it with the string "bba". If we have no "ab" as a substring,

452. Minimum Number of Arrows to Burst Balloons

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of s

Leetcode: Minimum Number of Arrows to Burst Balloons

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of s