-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathminimum-number-of-refueling-stops.py
93 lines (71 loc) · 2.7 KB
/
minimum-number-of-refueling-stops.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import heapq
from collections import defaultdict, deque
from typing import DefaultDict, Deque, Dict, List, Tuple
class Solution:
def minRefuelStops(
self, target: int, start_fuel: int, stations: List[List[int]]
) -> int:
pos = 0
refuels = 0
fuel = start_fuel
if fuel >= target:
return refuels
max_fuel: List[int] = []
while pos < len(stations):
while max_fuel and stations[pos][0] > fuel:
fuel += -heapq.heappop(max_fuel)
refuels += 1
if stations[pos][0] > fuel:
return -1
while pos < len(stations) and stations[pos][0] <= fuel:
heapq.heappush(max_fuel, -stations[pos][1])
pos += 1
if fuel >= target:
return refuels
while max_fuel and target > fuel:
fuel += -heapq.heappop(max_fuel)
refuels += 1
if fuel >= target:
return refuels
return -1
def minRefuelStopsDijkstra(
self, target: int, start_fuel: int, stations: List[List[int]]
) -> int:
if not stations:
return 0 if start_fuel >= target else -1
if start_fuel < stations[0][0]:
return -1
max_fuel: DefaultDict[
Tuple[int, int], int
] = defaultdict( # (pos, refuels) -> max fuel avail
lambda: -1
)
queue: List[Tuple[int, int, int]] = [
(0, 0, start_fuel - stations[0][0])
] # (pos, refuels, fuel_left)
while queue:
refuels, pos, fuel = heapq.heappop(queue)
pos = -pos
coordinate = min(
stations[pos][0] if pos < len(stations) else target, target
)
if coordinate >= target:
return refuels
next_coordinate = min(
stations[pos + 1][0] if pos + 1 < len(stations) else target, target
)
needed_fuel = next_coordinate - coordinate
if needed_fuel <= fuel:
fuel_left = fuel - needed_fuel
if fuel_left > max_fuel[(pos + 1, refuels)]:
heapq.heappush(queue, (refuels, -(pos + 1), fuel_left))
max_fuel[(pos + 1, refuels)] = fuel_left
if needed_fuel <= fuel + stations[pos][1]:
fuel_left = fuel + stations[pos][1] - needed_fuel
if fuel_left > max_fuel[(pos + 1, refuels + 1)]:
heapq.heappush(
queue,
(refuels + 1, -(pos + 1), fuel_left),
)
max_fuel[(pos + 1, refuels + 1)] = fuel_left
return -1