Created
February 21, 2021 09:22
-
-
Save samarthsewlani/726254e9547a47fb059168a2ff8a0b08 to your computer and use it in GitHub Desktop.
Truck Tour HackerRank Problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solve(petrol,distance,n): | |
start,tank=0,0 | |
for i in range(n): #Start from each index | |
start=i | |
tank=0 #Reset the tank for each iteration | |
for j in range(n): | |
current=(start+j)%n #To make the index in range for circular array | |
tank+=petrol[current]-distance[current] #Add the petrol and subtract the petrol required to reach next station | |
if tank<0: #If petrol in tank becomes negative, we cannot complete the loop | |
break | |
if j==n-1: #If we visit all n pumps, task is completed | |
return start | |
n=int(input()) | |
petrol,distance=[],[] | |
for i in range(n): | |
p,d=[int(x) for x in input().split()] | |
petrol.append(p),distance.append(d) | |
print(solve(petrol,distance,n)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment