-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathCircular Tour
More file actions
47 lines (37 loc) · 810 Bytes
/
Copy pathCircular Tour
File metadata and controls
47 lines (37 loc) · 810 Bytes
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
#include <bits/stdc++.h>
using namespace std;
class petrolPump
{
public:
int petrol;
int distance;
};
int printTour(petrolPump arr[], int n)
{
int start = 0;
int end = 1;
int curr_petrol = arr[start].petrol - arr[start].distance;
while (end != start || curr_petrol < 0)
{
while (curr_petrol < 0 && start != end)
{
curr_petrol -= arr[start].petrol - arr[start].distance;
start = (start + 1) % n;
if (start == 0)
return -1;
}
curr_petrol += arr[end].petrol - arr[end].distance;
end = (end + 1) % n;
}
return start;
}
int main()
{
petrolPump arr[] = {{6, 4}, {3, 6}, {7, 3}};
int n = sizeof(arr)/sizeof(arr[0]);
int start = printTour(arr, n);
(start == -1)? cout<<"No solution": cout<<"Start = "<<start;
return 0;
}
// Time COmplexity O(N)
// space Complexity O(1)