1
+ from dataclasses import dataclass
2
+
1
3
"""
2
4
Task:
3
5
There are n gas stations along a circular route, where the amount of gas
4
- at the ith station is gas [i].
6
+ at the ith station is gas_quantities [i].
5
7
6
- You have a car with an unlimited gas tank and it costs cost [i] of gas
7
- to travel from the ith station to its next (i + 1)th station. You begin the
8
- journey with an empty tank at one of the gas stations.
8
+ You have a car with an unlimited gas tank and it costs costs [i] of gas
9
+ to travel from the ith station to its next (i + 1)th station.
10
+ You begin the journey with an empty tank at one of the gas stations.
9
11
10
- Given two integer arrays gas and cost, return the starting gas station's index
11
- if you can travel around the circuit once in the clockwise direction,
12
- otherwise return -1. If there exists a solution, it is guaranteed to be unique
12
+ Given two integer arrays gas_quantities and costs, return the starting
13
+ gas station's index if you can travel around the circuit once
14
+ in the clockwise direction, otherwise return -1.
15
+ If there exists a solution, it is guaranteed to be unique
13
16
14
17
Reference: https://leetcode.com/problems/gas-station/description
15
18
16
19
Implementation notes:
17
20
First, check whether the total gas is enough to complete the journey. If not, return -1.
18
21
However, if there is enough gas, it is guaranteed that there is a valid
19
22
starting index to reach the end of the journey.
20
- Greedily calculate the net gain (gas - cost) at each station.
23
+ Greedily calculate the net gain (gas_quantity - cost) at each station.
21
24
If the net gain ever goes below 0 while iterating through the stations,
22
25
start checking from the next station.
23
26
24
27
"""
25
28
26
29
27
- def can_complete_journey (gas : list [int ], cost : list [int ]) -> int :
30
+ @dataclass
31
+ class GasStation :
32
+ gas_quantity : int
33
+ cost : int
34
+
35
+
36
+ def get_gas_stations (gas_quantities : list [int ], costs : list [int ]) -> list [GasStation ]:
37
+ """
38
+ This function returns a list of gas stations.
39
+
40
+ 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
43
+
44
+ Returns:
45
+ gas_stations [list]: a list of gas stations
46
+ """
47
+ gas_stations = [
48
+ GasStation (gas_quantity , cost )
49
+ for (gas_quantity , cost ) in zip (gas_quantities , costs )
50
+ ]
51
+ return gas_stations
52
+
53
+
54
+ def can_complete_journey (gas_quantities : list [int ], costs : list [int ]) -> int :
28
55
"""
29
56
This function returns the index from which to start the journey
30
57
in order to reach the end.
31
58
32
59
Args:
33
- gas [list]: Amount of gas available at each station
60
+ gas_quantities [list]: Amount of gas available at each station
34
61
cost [list]: The cost of gas required to move from a station to the next
35
62
36
63
Returns:
@@ -43,16 +70,18 @@ def can_complete_journey(gas: list[int], cost: list[int]) -> int:
43
70
-1
44
71
45
72
"""
46
- total_gas = sum (gas )
47
- total_cost = sum (cost )
73
+ total_gas = sum (gas_quantities )
74
+ total_cost = sum (costs )
48
75
49
76
if total_gas < total_cost :
50
77
return - 1
51
78
52
79
start = 0
53
80
net = 0
54
- for i in range (len (gas )):
55
- net += gas [i ] - cost [i ]
81
+ gas_stations = get_gas_stations (gas_quantities , costs )
82
+
83
+ for i , gas_station in enumerate (gas_stations ):
84
+ net += gas_station .gas_quantity - gas_station .cost
56
85
if net < 0 :
57
86
start = i + 1
58
87
net = 0
0 commit comments