-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularTour.java
More file actions
43 lines (34 loc) · 1.23 KB
/
CircularTour.java
File metadata and controls
43 lines (34 loc) · 1.23 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
package Queue;
public class CircularTour {
static int printTour(petrolPump[] arr, int n) {
int start = 0;
int end = 1;
int curr_petrol = arr[start].petrol - arr[start].distance;
// If current amount of petrol in truck becomes less than 0, then remove the starting petrol pump from tour
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;
}
public static void main(String[] args) {
petrolPump[] arr = {new petrolPump(6, 4),
new petrolPump(3, 6),
new petrolPump(7, 3)};
int start = printTour(arr, arr.length);
System.out.println(start == -1 ? "No Solution" : "Start = " + start);
}
static class petrolPump {
int petrol;
int distance;
public petrolPump(int petrol, int distance) {
this.petrol = petrol;
this.distance = distance;
}
}
}