Skip to content
Open
34 changes: 34 additions & 0 deletions data_structures/arrays/gas_station.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from typing import list

class Solution:

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

data_structures/arrays/gas_station.py:1:1: I001 Import block is un-sorted or un-formatted
def can_complete_circuit(self, gas: list[int], cost: list[int]) -> int:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/arrays/gas_station.py, please provide doctest for the function can_complete_circuit

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/arrays/gas_station.py, please provide doctest for the function can_complete_circuit

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/arrays/gas_station.py, please provide doctest for the function can_complete_circuit

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/arrays/gas_station.py, please provide doctest for the function can_complete_circuit

# Step 1: Initialize variables
total_gas = 0
current_gas = 0
start_station = 0

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/arrays/gas_station.py:9:1: W293 Blank line contains whitespace
# Step 2: Loop through each gas station
for i in range(len(gas)):
# Calculate the net gas gain/loss at the current station
total_gas += gas[i] - cost[i]
current_gas += gas[i] - cost[i]

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/arrays/gas_station.py:15:1: W293 Blank line contains whitespace
# Step 3: If current_gas becomes negative, it means we cannot continue
if current_gas < 0:
start_station = i + 1

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

data_structures/arrays/gas_station.py:18:38: W291 Trailing whitespace
current_gas = 0

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

data_structures/arrays/gas_station.py:19:32: W291 Trailing whitespace

# Step 4: Check if the total gas is enough to complete the circuit
if total_gas < 0:
return -1
else:
return start_station


# Example 1:
gas = [1, 2, 3, 4, 5]
cost = [3, 4, 5, 1, 2]

solution = Solution()
print(solution.can_complete_circuit(gas, cost)) # Output: 3

Loading