Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Gas Station
Bookmark
Input
Example 1
Reset mid-way
Impossible
Start at 0
Single station
Custom
gas
=
[1, 2, 3, 4, 5]
,
cost
=
[3, 4, 5, 1, 2]
gas
cost
net
0
1
2
3
4
1
2
3
4
5
3
4
5
1
2
gas
cost
net
0
1
2
3
4
1
2
3
4
5
3
4
5
1
2
current =
0
total =
0
gas
cost
net
0
1
2
3
4
1
2
3
4
5
3
4
5
1
2
i
start
total =
0
, current =
0
, start =
0
current =
-2
total =
-2
gas
cost
net
-2
0
1
2
3
4
1
2
3
4
5
3
4
5
1
2
i
start
net =
1
−
3
=
-2
→ current =
0
− 2
=
-2
current =
0
total =
-2
gas
cost
net
-2
0
1
2
3
4
1
2
3
4
5
3
4
5
1
2
i
start
current
-2 < 0
→ start =
1
, current =
0
current =
-2
total =
-4
gas
cost
net
-2
-2
0
1
2
3
4
1
2
3
4
5
3
4
5
1
2
i
start
net =
2
−
4
=
-2
→ current =
0
− 2
=
-2
current =
-2
total =
-6
gas
cost
net
-2
-2
-2
0
1
2
3
4
1
2
3
4
5
3
4
5
1
2
i
start
net =
3
−
5
=
-2
→ current =
0
− 2
=
-2
current =
0
total =
-6
gas
cost
net
-2
-2
-2
0
1
2
3
4
1
2
3
4
5
3
4
5
1
2
i
start
current
-2 < 0
→ start =
3
, current =
0
current =
3
total =
-3
gas
cost
net
-2
-2
-2
+3
0
1
2
3
4
1
2
3
4
5
3
4
5
1
2
i
start
net =
4
−
1
=
+3
→ current =
0
+ 3
=
3
current =
6
total =
0
gas
cost
net
-2
-2
-2
+3
+3
0
1
2
3
4
1
2
3
4
5
3
4
5
1
2
i
start
net =
5
−
2
=
+3
→ current =
3
+ 3
=
6
current =
6
total =
0
gas
cost
net
-2
-2
-2
+3
+3
0
1
2
3
4
1
2
3
4
5
3
4
5
1
2
start
total = 0 ≥ 0 →
start at station 3
algo
master
.
io
Step:
Start: Find a starting station to complete the circuit
0 / 10
Input
Example 1
Reset mid-way
Impossible
Start at 0
Single station
Custom
gas
=
[1, 2, 3, 4, 5]
,
cost
=
[3, 4, 5, 1, 2]
0 / 10
algo
master
.
io
Step:
Start: Find a starting station to complete the circuit