-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path!16. Circular Tour - v1.py
More file actions
47 lines (40 loc) · 1.45 KB
/
!16. Circular Tour - v1.py
File metadata and controls
47 lines (40 loc) · 1.45 KB
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
'''
lis[][0]:Petrol
lis[][1]:Distance
'''
class Solution:
# Function to find starting point where the truck can start to get through
# the complete circle without exhausting its petrol in between.
def tour(self, lis, n):
total_petrol = 0 # Total petrol available
total_distance = 0 # Total distance to cover
start_index = 0 # Starting index
current_petrol = 0 # Petrol at current pump
# Traverse all petrol pumps
for i in range(n):
total_petrol += lis[i][0]
total_distance += lis[i][1]
current_petrol += lis[i][0] - lis[i][1]
# If at any point, petrol at current pump is negative,
# reset starting index to next index and reset current petrol
if current_petrol < 0:
start_index = i + 1
current_petrol = 0
# If total petrol is less than total distance, no solution exists
if total_petrol < total_distance:
return -1
else:
return start_index
#{
# Driver Code Starts
if __name__ == '__main__':
t = int(input())
for i in range(t):
n = int(input())
arr=list(map(int, input().strip().split()))
lis=[]
for i in range(1, 2*n, 2):
lis.append([ arr[i-1], arr[i] ])
print(Solution().tour(lis, n))
# Contributed by: Harshit Sidhwa
# } Driver Code Ends