Skip to content

Commit 8478904

Browse files
nimishh7pre-commit-ci[bot]cclauss
authored
Added gas_station.py (#11822)
* Added gas_station.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update gas_station.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update gas_station.py changes done * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update gas_station.py * Update gas_station.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update gas_station.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update gas_station.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update gas_station.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * updating DIRECTORY.md * Refactor gas station circuit solution with NamedTuple * Clarify gas_stations argument description Updated the argument description for gas_stations to clarify the attributes. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: cclauss <cclauss@users.noreply.github.com>
1 parent db6bf8a commit 8478904

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

‎DIRECTORY.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@
264264
* Arrays
265265
* [Equilibrium Index In Array](data_structures/arrays/equilibrium_index_in_array.py)
266266
* [Find Triplets With 0 Sum](data_structures/arrays/find_triplets_with_0_sum.py)
267+
* [Gas Station](data_structures/arrays/gas_station.py)
267268
* [Index 2D Array In 1D](data_structures/arrays/index_2d_array_in_1d.py)
268269
* [Kth Largest Element](data_structures/arrays/kth_largest_element.py)
269270
* [Median Two Array](data_structures/arrays/median_two_array.py)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from typing import NamedTuple
2+
3+
4+
class GasStation(NamedTuple):
5+
gas: int # Amount of gas available at this gas station
6+
cost: int # Cost of gas required to drive to the next station
7+
8+
9+
def can_complete_circuit(gas_stations: list[GasStation]) -> int:
10+
"""
11+
Finds the starting station index to complete the circuit,
12+
or returns -1 if not possible.
13+
Args:
14+
gas_stations (List[GasStation]): List of gas stations with gas and cost.
15+
Returns:
16+
The index of the starting station, or -1 if no solution exists.
17+
Examples:
18+
>>> GS = GasStation
19+
>>> test_stations = (
20+
... [GS(1, 3), GS(2, 4), GS(3, 5), GS(4, 1), GS(5, 2)],
21+
... [GS(2, 3), GS(3, 4), GS(4, 3)],
22+
... [GS(5, 4), GS(1, 4), GS(2, 1), GS(3, 5), GS(4, 1)]
23+
... )
24+
>>> can_complete_circuit(test_stations[0])
25+
3
26+
>>> can_complete_circuit(test_stations[1])
27+
-1
28+
>>> can_complete_circuit(test_stations[2])
29+
4
30+
"""
31+
total_gas = sum(station.gas - station.cost for station in gas_stations)
32+
current_gas: int = 0
33+
start_station = 0
34+
for i, gas_station in enumerate(gas_stations):
35+
needed_gas = gas_station.gas - gas_station.cost
36+
total_gas += needed_gas
37+
current_gas += needed_gas
38+
if current_gas < 0:
39+
start_station = i + 1
40+
current_gas = 0
41+
if total_gas < 0:
42+
return -1
43+
return start_station
44+
45+
46+
# Example usage with doctests
47+
if __name__ == "__main__":
48+
import doctest
49+
50+
doctest.testmod()

0 commit comments

Comments
 (0)