1
- from dataclasses import dataclass
2
-
3
1
"""
4
2
Task:
5
3
There are n gas stations along a circular route, where the amount of gas
11
9
12
10
Given two integer arrays gas_quantities and costs, return the starting
13
11
gas station's index if you can travel around the circuit once
14
- in the clockwise direction, otherwise return -1.
12
+ in the clockwise direction otherwise, return -1.
15
13
If there exists a solution, it is guaranteed to be unique
16
14
17
15
Reference: https://leetcode.com/problems/gas-station/description
25
23
start checking from the next station.
26
24
27
25
"""
26
+ from dataclasses import dataclass
28
27
29
28
30
29
@dataclass
@@ -33,65 +32,60 @@ class GasStation:
33
32
cost : int
34
33
35
34
36
- def get_gas_stations (gas_quantities : list [int ], costs : list [int ]) -> list [GasStation ]:
35
+ def get_gas_stations (gas_quantities : list [int ], costs : list [int ]) -> tuple [GasStation ]:
37
36
"""
38
- This function returns a list of gas stations.
37
+ This function returns a tuple of gas stations.
39
38
40
39
Args:
41
- gas_quantities [list] : Amount of gas available at each station
42
- cost [list] : The cost of gas required to move from a station to the next
40
+ gas_quantities: Amount of gas available at each station
41
+ costs : The cost of gas required to move from one station to the next
43
42
44
43
Returns:
45
- gas_stations [list]: a list of gas stations
46
-
47
- Examples:
48
- >>> get_gas_stations([1, 2, 3, 4, 5], [3, 4, 5, 1, 2])
49
- [GasStation(gas_quantity=1, cost=3), GasStation(gas_quantity=2, cost=4), \
50
- GasStation(gas_quantity=3, cost=5), GasStation(gas_quantity=4, cost=1), \
51
- GasStation(gas_quantity=5, cost=2)]
44
+ A tuple of gas stations
45
+
46
+ >>> gas_stations = get_gas_stations([1, 2, 3, 4, 5], [3, 4, 5, 1, 2])
47
+ >>> len(gas_stations)
48
+ 5
49
+ >>> gas_stations[0]
50
+ GasStation(gas_quantity=1, cost=3)
51
+ >>> gas_stations[-1]
52
+ GasStation(gas_quantity=5, cost=2)
52
53
"""
53
- gas_stations = [
54
- GasStation (gas_quantity , cost )
55
- for (gas_quantity , cost ) in zip (gas_quantities , costs )
56
- ]
57
- return gas_stations
54
+ return tuple (
55
+ GasStation (quantity , cost ) for quantity , cost in zip (gas_quantities , costs )
56
+ )
58
57
59
58
60
- def can_complete_journey (gas_quantities : list [ int ], costs : list [ int ]) -> int :
59
+ def can_complete_journey (gas_stations : tuple [ GasStation ]) -> int :
61
60
"""
62
61
This function returns the index from which to start the journey
63
62
in order to reach the end.
64
63
65
64
Args:
66
65
gas_quantities [list]: Amount of gas available at each station
67
- cost [list]: The cost of gas required to move from a station to the next
66
+ cost [list]: The cost of gas required to move from one station to the next
68
67
69
68
Returns:
70
69
start [int]: start index needed to complete the journey
71
70
72
71
Examples:
73
- >>> can_complete_journey([1, 2, 3, 4, 5], [3, 4, 5, 1, 2])
72
+ >>> can_complete_journey(get_gas_stations( [1, 2, 3, 4, 5], [3, 4, 5, 1, 2]) )
74
73
3
75
- >>> can_complete_journey([2, 3, 4], [3, 4, 3])
74
+ >>> can_complete_journey(get_gas_stations( [2, 3, 4], [3, 4, 3]) )
76
75
-1
77
-
78
76
"""
79
- total_gas = sum (gas_quantities )
80
- total_cost = sum (costs )
81
-
77
+ total_gas = sum (gas_station .gas_quantity for gas_station in gas_stations )
78
+ total_cost = sum (gas_station .cost for gas_station in gas_stations )
82
79
if total_gas < total_cost :
83
80
return - 1
84
81
85
82
start = 0
86
83
net = 0
87
- gas_stations = get_gas_stations (gas_quantities , costs )
88
-
89
84
for i , gas_station in enumerate (gas_stations ):
90
85
net += gas_station .gas_quantity - gas_station .cost
91
86
if net < 0 :
92
87
start = i + 1
93
88
net = 0
94
-
95
89
return start
96
90
97
91
0 commit comments