Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod

class Solution:
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
"""
Find the starting gas station to complete the circuit.

Key insights:
1. If total gas < total cost, impossible to complete circuit
2. If we can't reach station j from station i, then we can't reach j
from any station between i and j (they all have negative tank balance)
3. If a solution exists, it's guaranteed to be unique

Strategy: Greedy one-pass
- Track total gas surplus/deficit
- Track current tank from potential starting point
- If tank goes negative, reset start to next station

Time: O(n), Space: O(1)
"""

n = len(gas)
total_gas = 0 # Total gas surplus/deficit for entire circuit
current_tank = 0 # Current gas in tank from start_station
start_station = 0 # Potential starting station

for i in range(n):
# Calculate net gas at this station (gain - cost to next)
net_gas = gas[i] - cost[i]

# Add to both total and current tank
total_gas += net_gas
current_tank += net_gas

# If current tank is negative, we can't reach next station from start_station
# All stations from start_station to i are invalid starting points
# Try starting from the next station (i + 1)
if current_tank < 0:
start_station = i + 1 # Reset starting point
current_tank = 0 # Reset tank

# If total gas is negative, impossible to complete circuit
# Otherwise, start_station is the answer
return start_station if total_gas >= 0 else -1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function canCompleteCircuit(gas: number[], cost: number[]): number {
const n = gas.length;
let totalGas = 0;
let currentTank = 0;
let startStation = 0;

for (let i = 0; i < n; i++) {
const netGas = gas[i] - cost[i];
totalGas += netGas;
currentTank += netGas;

if (currentTank < 0) {
startStation = i + 1;
currentTank = 0;
}
}

return totalGas >= 0 ? startStation : -1;
};
18 changes: 18 additions & 0 deletions tests/test_150_questions_round_23/test_14_gas_station_round_23.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import unittest
from src.my_project.interviews.top_150_questions_round_23\
.ex_14_gas_station import Solution

class GasStationTestCase(unittest.TestCase):

def test_gas_station_first_case(self):
solution = Solution()
output = solution.canCompleteCircuit(gas = [1,2,3,4,5],
cost = [3,4,5,1,2])
target = 3
self.assertEqual(output, target)

def test_gas_station_second_case(self):
solution = Solution()
output = solution.canCompleteCircuit(gas = [2,3,4], cost = [3,4,3])
target = -1
self.assertEqual(output, target)
Loading