LeetCode: Gas Station problem

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 = nextStation
if 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

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

🚀 Boosted Finance: Strategist Competition Kickoff

PowerVC — Redhat and Centos 8 Image creation with pvsadm

How commands in linux shell work, review of “ls -l” example.

Adding properties to custom GTK4 widgets in Rust

How We’ve Progressed

Lenovo ThinkPad P15s Intel Core i7–10610U 32GB 1TB SSD NVIDIA Premier Warranty

Solving circular array rotation

NixOs native flake deployment with LUKS drive encryption and LVM

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
King Chun O

King Chun O

More from Medium

Chainlink and interfaces

DeFi Kingdoms

Super friendly introduction to classnames in React

CS373 Spring 2022: Week 14