Skip to content

Commit f27c35b

Browse files
committed
Add Gas Station solution README
1 parent 6391ce7 commit f27c35b

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

arrays/134_Gas_Station/README.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Gas Station
2+
3+
## Problem Description
4+
5+
There are n gas stations along a circular route, where the amount of gas at the ith station is `gas[i]`.
6+
7+
You have a car with an unlimited gas tank and it costs `cost[i]` of gas to travel from the `i`th station
8+
to its next `(i + 1)`th station. You begin the journey with an empty tank at one of the gas stations.
9+
10+
Given two integer arrays `gas` and `cost`, return the starting gas station's index if you can travel
11+
around the circuit once in the clockwise direction, otherwise return -1. If there exists a solution,
12+
it is guaranteed to be unique.
13+
14+
**Example 1:**
15+
16+
* Input: `gas = [1,2,3,4,5], cost = [3,4,5,1,2]`
17+
* Output: `3`
18+
* Explanation:
19+
* Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
20+
* Travel to station 4. Your tank = 4 - 1 + 5 = 8
21+
* Travel to station 0. Your tank = 8 - 2 + 1 = 7
22+
* Travel to station 1. Your tank = 7 - 3 + 2 = 6
23+
* Travel to station 2. Your tank = 6 - 4 + 3 = 5
24+
* Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3.
25+
* Therefore, return 3 as the starting index.
26+
27+
**Example 2:**
28+
29+
* Input: `gas = [2,3,4], cost = [3,4,3]`
30+
* Output: `-1`
31+
* Explanation:
32+
* You can't start at station 0 or 1, as there is not enough gas to travel to the next station.
33+
* Let's start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
34+
* Travel to station 0. Your tank = 4 - 3 + 2 = 3
35+
* Travel to station 1. Your tank = 3 - 3 + 3 = 3
36+
* You cannot travel back to station 2, as it requires 4 unit of gas but you only have 3.
37+
* Therefore, you can't travel around the circuit once no matter where you start.
38+
39+
**Constraints:**
40+
41+
42+
* `n == gas.length == cost.length`
43+
* `1 <= n <= 10^5`
44+
* `0 <= gas[i], cost[i] <= 10^4`
45+
* The input is generated such that the answer is unique.
46+
47+
48+
## Solution
49+
50+
```python
51+
def can_complete_circuit(gas: list[int], cost: list[int]) -> int:
52+
"""
53+
Determine the starting gas station's index to travel around a circular route once.
54+
55+
The car starts with an empty tank at one of the gas stations and has an unlimited gas capacity.
56+
It must be able to travel clockwise around all stations, refueling at each, without running out of gas.
57+
58+
:param gas: A list of integers where gas[i] represents the amount of gas at station i.
59+
:param cost: A list of integers where cost[i] represents the gas cost to travel from station i to i+1.
60+
:return: The index of the starting gas station if the circuit can be completed, otherwise -1.
61+
"""
62+
if sum(gas) < sum(cost):
63+
return -1
64+
length = len(gas)
65+
current_tank = 0
66+
start = -1
67+
for index in range(length):
68+
current_tank += gas[index] - cost[index]
69+
if current_tank < 0:
70+
start = -1
71+
current_tank = 0
72+
elif start == -1:
73+
start = index
74+
return start
75+
```
76+
77+
* **Time Complexity:** $O(n)$
78+
* **Space Complexity:** $O(1)$
79+
80+
## Explanation of the Solution
81+
82+
This problem involves finding a starting gas station in a circular route such that a car can travel
83+
around all stations without running out of gas. The solution efficiently checks for the valid starting
84+
point using a greedy approach with cumulative gas tracking.
85+
86+
1. Early Termination Check:
87+
* If total gas is insufficient, return `-1` immediately.
88+
2. Initialize Variables:
89+
* `current_tank = 0` → Tracks gas balance during the journey.
90+
* `start = -1` → Stores the candidate starting station.
91+
3. Iterate Through Stations:
92+
* For each station `i`, update `current_tank += gas[i] - cost[i]` (gas added minus gas spent).
93+
* If `current_tank < 0`:
94+
* The current candidate station (`start`) is invalid.
95+
* Reset `current_tank = 0` and mark `start = -1` (search for a new candidate).
96+
* If `current_tank >= 0` and no candidate is set (`start == -1`):
97+
* Set `start = i` (this station is a potential starting point).
98+
4. Return the Valid Start:
99+
* After the loop, start holds the unique valid station (since `sum(gas) >= sum(cost)` guarantees a solution).
100+
101+
By resetting `current_tank` when it goes negative, we ensure the candidate station can cover all subsequent stations
102+
without deficits.
103+
104+
The loop checks stations in order, and the `sum(gas) >= sum(cost)` condition ensures the remaining stations (after the
105+
candidate) can compensate for any prior deficits.

0 commit comments

Comments
 (0)