Skip to content

Commit 0207354

Browse files
authored
Update gas_station.py
1 parent 06acf54 commit 0207354

File tree

1 file changed

+24
-30
lines changed

1 file changed

+24
-30
lines changed

Diff for: greedy_methods/gas_station.py

+24-30
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from dataclasses import dataclass
2-
31
"""
42
Task:
53
There are n gas stations along a circular route, where the amount of gas
@@ -11,7 +9,7 @@
119
1210
Given two integer arrays gas_quantities and costs, return the starting
1311
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.
1513
If there exists a solution, it is guaranteed to be unique
1614
1715
Reference: https://leetcode.com/problems/gas-station/description
@@ -25,6 +23,7 @@
2523
start 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

Comments
 (0)