Skip to content

Commit 13564ec

Browse files
committed
Fix 0134-gas-station.py
1 parent 67ef614 commit 13564ec

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

python/0134-gas-station.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
class Solution:
22
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
3-
start, end = len(gas) - 1, 0
4-
total = gas[start] - cost[start]
3+
if sum(gas) < sum(cost):
4+
return -1
5+
6+
total = 0
7+
res = 0
8+
for i in range(len(gas)):
9+
total += (gas[i] - cost[i])
510

6-
while start >= end:
7-
while total < 0 and start >= end:
8-
start -= 1
9-
total += gas[start] - cost[start]
10-
if start == end:
11-
return start
12-
total += gas[end] - cost[end]
13-
end += 1
14-
return -1
11+
if total < 0:
12+
total = 0
13+
res = i + 1
14+
15+
return res

0 commit comments

Comments
 (0)