1+ from dataclasses import dataclass
2+
13"""
24Task:
35There 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].
57
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.
911
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
1316
1417Reference: https://leetcode.com/problems/gas-station/description
1518
1619Implementation notes:
1720First, check whether the total gas is enough to complete the journey. If not, return -1.
1821However, if there is enough gas, it is guaranteed that there is a valid
1922starting 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.
2124If the net gain ever goes below 0 while iterating through the stations,
2225start checking from the next station.
2326
2427"""
2528
2629
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 :
2855 """
2956 This function returns the index from which to start the journey
3057 in order to reach the end.
3158
3259 Args:
33- gas [list]: Amount of gas available at each station
60+ gas_quantities [list]: Amount of gas available at each station
3461 cost [list]: The cost of gas required to move from a station to the next
3562
3663 Returns:
@@ -43,16 +70,18 @@ def can_complete_journey(gas: list[int], cost: list[int]) -> int:
4370 -1
4471
4572 """
46- total_gas = sum (gas )
47- total_cost = sum (cost )
73+ total_gas = sum (gas_quantities )
74+ total_cost = sum (costs )
4875
4976 if total_gas < total_cost :
5077 return - 1
5178
5279 start = 0
5380 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
5685 if net < 0 :
5786 start = i + 1
5887 net = 0
0 commit comments