Skip to content

Commit 7b0bd4a

Browse files
committed
day 12
1 parent 9f32925 commit 7b0bd4a

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
Minimum Number of Refueling Stops
3+
=================================
4+
5+
A car travels from a starting position to a destination which is target miles east of the starting position.
6+
7+
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.
8+
9+
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.
10+
11+
When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car.
12+
13+
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.
14+
15+
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.
16+
17+
Example 1:
18+
Input: target = 1, startFuel = 1, stations = []
19+
Output: 0
20+
Explanation: We can reach the target without refueling.
21+
22+
Example 2:
23+
Input: target = 100, startFuel = 1, stations = [[10,100]]
24+
Output: -1
25+
Explanation: We can't reach the target (or even the first gas station).
26+
Example 3:
27+
Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]]
28+
Output: 2
29+
Explanation:
30+
We start with 10 liters of fuel.
31+
We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas.
32+
Then, we drive from position 10 to position 60 (expending 50 liters of fuel),
33+
and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target.
34+
We made 2 refueling stops along the way, so we return 2.
35+
36+
Note:
37+
1 <= target, startFuel, stations[i][1] <= 10^9
38+
0 <= stations.length <= 500
39+
0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target
40+
*/
41+
42+
class Solution
43+
{
44+
public:
45+
int minRefuelStops(int dest, int fuel, vector<vector<int>> &arr)
46+
{
47+
arr.push_back({dest, 0});
48+
int ans = 0, prev = 0;
49+
50+
priority_queue<int> canFill;
51+
52+
for (int i = 0; i < arr.size(); ++i)
53+
{
54+
int dist = arr[i][0] - prev;
55+
prev = arr[i][0];
56+
57+
if (fuel >= dist)
58+
{
59+
// we had sufficient fuel previously to come till here
60+
}
61+
else
62+
{
63+
// mtlb fuel km tha previously,
64+
// so to come here he must have refilled at previously visited station
65+
while (canFill.size() && fuel < dist)
66+
{
67+
fuel += canFill.top();
68+
canFill.pop();
69+
ans++;
70+
}
71+
72+
if (fuel < dist)
73+
return -1;
74+
}
75+
76+
fuel -= dist;
77+
canFill.push(arr[i][1]);
78+
}
79+
return ans;
80+
}
81+
};

Leetcode Daily Challenge/June-2021/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
| 9. | [Jump Game VI](https://leetcode.com/explore/challenge/card/june-leetcoding-challenge-2021/604/week-2-june-8th-june-14th/3773/) [cpp](./09.%20Jump%20Game%20VI.cpp) |
1414
| 10. | [My Calendar I](https://leetcode.com/explore/challenge/card/june-leetcoding-challenge-2021/604/week-2-june-8th-june-14th/3774/) [cpp](./10.%20My%20Calendar%20I.cpp) |
1515
| 11. | [Stone Game VII](https://leetcode.com/explore/challenge/card/june-leetcoding-challenge-2021/604/week-2-june-8th-june-14th/3775/) [cpp](./11.%20Stone%20Game%20VII.cpp) |
16+
| 12. | [Minimum Number of Refueling Stops](https://leetcode.com/explore/challenge/card/june-leetcoding-challenge-2021/604/week-2-june-8th-june-14th/3776/) [cpp](./12.%20Minimum%20Number%20of%20Refueling%20Stops.cpp) |

0 commit comments

Comments
 (0)