Problem :

https://www.hackerrank.com/challenges/truck-tour/problem


My Solution :

#!/usr/bin/env python3


def truck_tour(petrol_pumps):
    petrol_sum = distance_sum = i = j = 0
    while True:
        if distance_sum + petrol_pumps[j % n][1] <= petrol_sum + petrol_pumps[j % n][0]:
            petrol_sum += petrol_pumps[j % n][0]
            distance_sum += petrol_pumps[j % n][1]
            j += 1
        else:
            petrol_sum -= petrol_pumps[i][0]
            distance_sum -= petrol_pumps[i][1]
            i += 1
        if n <= i:
            raise Exception('Can not find answer!')
        if i + n == j:
            return i


n = int(input())
petrol_pumps = []
for _ in range(n):
    petrol_pumps.append(list(map(int, input().rstrip().split())))
result = truck_tour(petrol_pumps)
print(result)