Skip to content

Commit 0282a3b

Browse files
committed
feat: add solution for gas station in python
1 parent 3a22d96 commit 0282a3b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

alternative/medium/gas_station.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import List
2+
3+
def canCompleteCircuit(gas: List[int], cost: List[int]) -> int:
4+
if sum(gas) < sum(cost):
5+
return -1
6+
7+
curSum, res = 0, 0
8+
for i in range(0, len(gas)):
9+
curSum += gas[i] - cost[i]
10+
if curSum < 0:
11+
curSum = 0
12+
res = i + 1
13+
14+
return res
15+
16+
assert canCompleteCircuit([5,8,2,8], [6,5,6,6]) == 3
17+
assert canCompleteCircuit([1, 2, 3, 4, 5], [3, 4, 5, 1, 2]) == 3
18+
assert canCompleteCircuit([2, 3, 4], [3, 4, 3]) == -1
19+
assert canCompleteCircuit([3, 1, 1], [1, 2, 2]) == 0
20+
assert canCompleteCircuit([1, 2, 3], [1, 2, 3]) == 0
21+
assert canCompleteCircuit([2, 3, 4], [3, 4, 4]) == -1

0 commit comments

Comments
 (0)