forked from animesh0353/Hackerrank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTruck Tour
63 lines (57 loc) · 1.4 KB
/
Truck Tour
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
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Solution{
public static class Pump
{
int petrol;
int distance;
int extra;
public Pump(int petrol, int distance) {
// TODO Auto-generated constructor stub
this.petrol = petrol;
this.distance = distance;
extra = petrol - distance;
}
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = Integer.parseInt(in.nextLine());
String string;
String[] str;
Pump[] pumpArray = new Pump[n];
for (int i = 0; i < n; i++)
{
string = in.nextLine();
str = string.split(" ");
int p = Integer.parseInt(str[0]);
int d = Integer.parseInt(str[1]);
pumpArray[i] = new Pump(p, d);
}
Queue<Pump> queue = new LinkedList<Solution.Pump>();
int index = 1;
int current = 0;
queue.add(pumpArray[0]);
int remaining = pumpArray[0].extra;
while(queue.size() != n)
{
Pump pump;
if(remaining < 0)
{
pump = queue.remove();
remaining -= pump.extra;
current++;
current %= n;
}
else
{
pump = pumpArray[index++];
remaining += pump.extra;
queue.add(pump);
index %= n;
}
}
System.out.println(current);
}
}