-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path134.gas-station.py
39 lines (31 loc) · 909 Bytes
/
134.gas-station.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#
# @lc app=leetcode id=134 lang=python3
#
# [134] Gas Station
#
# @lc code=start
class Solution:
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
n = len(gas)
# impossible condition
if sum(gas) - sum(cost) < 0: # when > 0, does it must have a solution?
return -1
start_point = 0
sm = 0
for i in range(n):
sm = sm + gas[i] - cost[i]
if sm < 0:
start_point = i + 1
sm = 0
return start_point
# while start_point < n:
# sm = 0
# for i in range(start_point, n):
# sm = sm + gas[i] - cost[i]
# if sm < 0:
# start_point = i + 1
# break
# if sm >= 0:
# break
# return start_point
# @lc code=end