Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions 134-Gas-Station.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
class Solution:
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
start, end = len(gas) - 1, 0
total = gas[start] - cost[start]
if sum(gas) < sum(cost):
return -1

res = 0
total = 0
for i in range(len(gas)):
total += gas[i] - cost[i]

if total < 0:
total = 0
res = i + 1

while start >= end:
while total < 0 and start >= end:
start -= 1
total += gas[start] - cost[start]
if start == end:
return start
total += gas[end] - cost[end]
end += 1
return -1
return res
23 changes: 12 additions & 11 deletions python/134-Gas-Station.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
class Solution:
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
start, end = len(gas) - 1, 0
total = gas[start] - cost[start]
if sum(gas) < sum(cost):
return -1

res = 0
total = 0
for i in range(len(gas)):
total += gas[i] - cost[i]

if total < 0:
total = 0
res = i + 1

while start >= end:
while total < 0 and start >= end:
start -= 1
total += gas[start] - cost[start]
if start == end:
return start
total += gas[end] - cost[end]
end += 1
return -1
return res