-
Notifications
You must be signed in to change notification settings - Fork 111
/
solution.java
82 lines (66 loc) · 2.59 KB
/
solution.java
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
//make a class to contain both the features of a petrolpump
class Pair{
public int capacity;
public int dist;
}
public class Solution {
/*
* Complete the truckTour function below.
*/
static int truckTour(int[][] petrolpumps) {
Queue<Pair> q = new LinkedList<Pair>();
//adding each node of petrolpumps to the queue 'q'
for(int i=0;i<petrolpumps.length;i++){
Pair temp = new Pair();
temp.capacity = petrolpumps[i][0];
temp.dist = petrolpumps[i][1];
q.offer(temp);
}
//start-> index of the petrolpump from where to start
//current-> index w.r.t. start index
//petrol-> net petrol left in the tank
int current=0, start=0, petrol=0;
while(current<petrolpumps.length){
Pair temp = q.poll();
petrol += temp.capacity;
//if petrol left is less than the distance to travel
if(petrol<temp.dist){
//update the start index to the next node
start += current+1;
//set current index and net petrol to zero
current=0;
petrol=0;
}
else{
//if we have enough petrol then set the net petrol for the next node
petrol-=temp.dist;
current++;
}
//insert this node to the end of the queue
q.offer(temp);
}
return start;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int n = Integer.parseInt(scanner.nextLine().trim());
int[][] petrolpumps = new int[n][2];
for (int petrolpumpsRowItr = 0; petrolpumpsRowItr < n; petrolpumpsRowItr++) {
String[] petrolpumpsRowItems = scanner.nextLine().split(" ");
for (int petrolpumpsColumnItr = 0; petrolpumpsColumnItr < 2; petrolpumpsColumnItr++) {
int petrolpumpsItem = Integer.parseInt(petrolpumpsRowItems[petrolpumpsColumnItr].trim());
petrolpumps[petrolpumpsRowItr][petrolpumpsColumnItr] = petrolpumpsItem;
}
}
int result = truckTour(petrolpumps);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedWriter.close();
}
}