LeetCode: Gas Station problem
Gas Station
Time Limit Exceeded
32 / 35 test cases passed.
class Solution:
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
results = []
if len(gas) == len(cost):
if len(gas) > 0:
if len(gas) <= math.pow(10, 5):
for i in range(len(gas)):
if gas[i] >= cost[i]:
currentStation = i
tankRemain = gas[currentStation]
maxStation = len(cost) - 1
for j in range(len(gas)):
nextStation = currentStation + 1
if nextStation > maxStation:
nextStation = nextStation - maxStation - 1
tankRemain = tankRemain - cost[currentStation]
if tankRemain >= 0:
tankRemain = tankRemain + gas[nextStation]
else:
results.append(-1)
currentStation = nextStationif tankRemain >= 0:
results.append(i)
else:
results.append(-1)
exist = 0
for x in range(len(results)):
if results[x] >= 0:
exist = exist + 1
if exist > 0:
for x in range(len(results)):
if results[x] >= 0:
return results[x]
else:
return -1
else:
return -1
else:
return -1
else:
return -1