We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 67ef614 commit 13564ecCopy full SHA for 13564ec
python/0134-gas-station.py
@@ -1,14 +1,15 @@
1
class Solution:
2
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
3
- start, end = len(gas) - 1, 0
4
- total = gas[start] - cost[start]
+ if sum(gas) < sum(cost):
+ return -1
5
+
6
+ total = 0
7
+ res = 0
8
+ for i in range(len(gas)):
9
+ total += (gas[i] - cost[i])
10
- while start >= end:
- while total < 0 and start >= end:
- start -= 1
- total += gas[start] - cost[start]
- if start == end:
11
- return start
12
- total += gas[end] - cost[end]
13
- end += 1
14
- return -1
+ if total < 0:
+ res = i + 1
15
+ return res
0 commit comments