-
Notifications
You must be signed in to change notification settings - Fork 0
/
truck_tour.py
51 lines (38 loc) · 1.01 KB
/
truck_tour.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/python3
"""
Solution to this problem:
https://www.hackerrank.com/challenges/truck-tour/problem
"""
#
# 3
# 1 5
# 10 3
# 3 4
import math
import os
import random
import re
import sys
def truckTour(petrolpumps):
"""
Finds the smallest index of the petrol pump from which we can start the tour.
"""
start, tank = 0, 0
for i in range(len(petrolpumps)):
amount_of_petrol, distance_to_next_pump = petrolpumps[i]
tank += amount_of_petrol - distance_to_next_pump
# at any moment, if the tank is empty we find out that start position
# should be at least in the next pump
if tank < 0:
start = i + 1
tank = 0
return start
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input().strip())
petrolpumps = []
for _ in range(n):
petrolpumps.append(list(map(int, input().rstrip().split())))
result = truckTour(petrolpumps)
fptr.write(str(result) + '\n')
fptr.close()