Skip to content
Open
Changes from 1 commit
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
32 changes: 32 additions & 0 deletions data_structures/arrays/gas_station.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from typing import List

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

data_structures/arrays/gas_station.py:1:1: UP035 `typing.List` is deprecated, use `list` instead

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 canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N802)

data_structures/arrays/gas_station.py:4:9: N802 Function name `canCompleteCircuit` should be lowercase

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

data_structures/arrays/gas_station.py:4:39: UP006 Use `list` instead of `List` for type annotation

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

data_structures/arrays/gas_station.py:4:56: UP006 Use `list` instead of `List` for type annotation
# Step 1: Initialize variables
# total_gas will keep track of the total balance of gas - cost across all stations

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/gas_station.py:6:89: E501 Line too long (90 > 88)
# current_gas will track the gas balance for the current trip from the starting station

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/gas_station.py:7:89: E501 Line too long (95 > 88)
# start_station will track the potential starting point of the circuit
total_gas = 0
current_gas = 0
start_station = 0

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/arrays/gas_station.py:12: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 18 in data_structures/arrays/gas_station.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/arrays/gas_station.py:18:1: W293 Blank line contains whitespace
# Step 3: If current_gas becomes negative, it means we cannot continue
# the journey from the current start_station, so we update the start_station
# to the next one and reset the current_gas to 0.
if current_gas < 0:
start_station = i + 1 # Move the starting station to the next one
current_gas = 0 # Reset the gas for the new start

# Step 4: Check if the total gas is enough to complete the circuit
# If total_gas is negative, it means the entire circuit cannot be completed
if total_gas < 0:
return -1
else:
# If total_gas is non-negative, return the start_station as the answer
return start_station
Loading