17 lines
404 B
Python
17 lines
404 B
Python
def can_complete_circuit(gas, cost):
|
|
if sum(gas) < sum(cost):
|
|
return -1
|
|
start = 0
|
|
bal = 0
|
|
for i in range(len(gas)):
|
|
bal += gas[i] - cost[i]
|
|
if bal < 0:
|
|
start = i + 1
|
|
bal = 0
|
|
return start
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print(can_complete_circuit([1, 2, 3, 4, 5], [3, 4, 5, 1, 2]))
|
|
print(can_complete_circuit([2, 3, 4], [3, 4, 3]))
|