Skip to content
Open
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions data_structures/arrays/gas_station.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class Solution:
def can_omplete_circuit(self, gas: list[int], cost: list[int]) -> int:
"""
Finds the starting station index to complete the circuit,

Check failure on line 4 in data_structures/arrays/gas_station.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

data_structures/arrays/gas_station.py:4:66: W291 Trailing whitespace
or returns -1 if not possible.
Args:
gas (List[int]): List of gas available at each station.
cost (List[int]): List of gas costs to travel to the next station.
Returns:
int: The index of the starting station, or -1 if no solution exists.
Examples:
>>> solution = Solution()
>>> solution.can_omplete_circuit([1, 2, 3, 4, 5], [3, 4, 5, 1, 2])
3
>>> solution.can_omplete_circuit([2, 3, 4], [3, 4, 3])
-1
>>> solution.can_omplete_circuit([5, 1, 2, 3, 4], [4, 4, 1, 5, 1])
4
"""
total_gas = 0
current_gas = 0
start_station = 0

for i in range(len(gas)):
total_gas += gas[i] - cost[i]
current_gas += gas[i] - cost[i]

if current_gas < 0:
start_station = i + 1
current_gas = 0

if total_gas < 0:
return -1
else:
return start_station


# Example usage with doctests
if __name__ == "__main__":
import doctest

doctest.testmod()
Loading