1- from dataclasses import dataclass
2-
31"""
42Task:
53There are n gas stations along a circular route, where the amount of gas
119
1210Given two integer arrays gas_quantities and costs, return the starting
1311gas 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.
1513If there exists a solution, it is guaranteed to be unique
1614
1715Reference: https://leetcode.com/problems/gas-station/description
2523start checking from the next station.
2624
2725"""
26+ from dataclasses import dataclass
2827
2928
3029@dataclass
@@ -33,65 +32,60 @@ class GasStation:
3332 cost : int
3433
3534
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 ]:
3736 """
38- This function returns a list of gas stations.
37+ This function returns a tuple of gas stations.
3938
4039 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
4342
4443 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)
5253 """
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+ )
5857
5958
60- def can_complete_journey (gas_quantities : list [ int ], costs : list [ int ]) -> int :
59+ def can_complete_journey (gas_stations : tuple [ GasStation ]) -> int :
6160 """
6261 This function returns the index from which to start the journey
6362 in order to reach the end.
6463
6564 Args:
6665 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
6867
6968 Returns:
7069 start [int]: start index needed to complete the journey
7170
7271 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]) )
7473 3
75- >>> can_complete_journey([2, 3, 4], [3, 4, 3])
74+ >>> can_complete_journey(get_gas_stations( [2, 3, 4], [3, 4, 3]) )
7675 -1
77-
7876 """
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 )
8279 if total_gas < total_cost :
8380 return - 1
8481
8582 start = 0
8683 net = 0
87- gas_stations = get_gas_stations (gas_quantities , costs )
88-
8984 for i , gas_station in enumerate (gas_stations ):
9085 net += gas_station .gas_quantity - gas_station .cost
9186 if net < 0 :
9287 start = i + 1
9388 net = 0
94-
9589 return start
9690
9791
0 commit comments