-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0871-minimum-number-of-refueling-stops.py
More file actions
40 lines (35 loc) · 1.06 KB
/
0871-minimum-number-of-refueling-stops.py
File metadata and controls
40 lines (35 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# time complexity: O(nlogn)
# space complexity: O(n)
from heapq import heappop, heappush
from typing import List
class Solution:
def minRefuelStops(self, target: int, startFuel: int, stations: List[List[int]]) -> int:
if startFuel >= target:
return 0
i = 0
n = len(stations)
maxHeap = []
maxDistance = startFuel
stops = 0
while maxDistance < target:
if i < n and stations[i][0] <= maxDistance:
heappush(maxHeap, -stations[i][1])
i += 1
elif not maxHeap:
return -1
else:
maxDistance += -heappop(maxHeap)
stops += 1
return stops
target = 1
startFuel = 1
stations = []
print(Solution().minRefuelStops(target, startFuel, stations))
target = 100
startFuel = 1
stations = [[10, 100]]
print(Solution().minRefuelStops(target, startFuel, stations))
target = 100
startFuel = 10
stations = [[10, 60], [20, 30], [30, 30], [60, 40]]
print(Solution().minRefuelStops(target, startFuel, stations))