Skip to content

Commit 93c46f7

Browse files
committed
I suck at competitive programming - google kickstart round f atm queue
1 parent 8709467 commit 93c46f7

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2+
import java.util.*;
3+
import java.io.*;
4+
public class Solution {
5+
public static void main(String args[]) {
6+
7+
// int[] order = new int[] {2, 7, 4};
8+
// int max = 3;
9+
// List<Integer> findOrder = orderCalculator(order, max);
10+
// for (int num: findOrder) {
11+
// System.out.println(num);
12+
// }
13+
Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
14+
int t = in.nextInt(); // Scanner has functions to read ints, longs, strings, chars, etc.
15+
// int t = 1;
16+
for (int i = 1; i <= t; ++i) {
17+
// Reading the test cases
18+
int n = in.nextInt(); // Number of people standing in the queue
19+
int m = in.nextInt(); // Max amount
20+
int[] order = new int[n];
21+
for (int j = 0; j < n; j++) {
22+
// add next int to array
23+
order[j] = in.nextInt();
24+
// System.out.println("Order at " + j + " " + order[j]);
25+
}
26+
List<Integer> result = orderCalculator(order, m);
27+
StringBuilder answer = new StringBuilder();
28+
answer.append("Case #" + i + ": ");
29+
30+
result.forEach(nb -> answer.append(nb + " "));
31+
System.out.println(answer.toString());
32+
}
33+
}
34+
public static List<Integer> orderCalculator(int[] order, int x) {
35+
// Create an actual queue;
36+
Queue<Integer> queue = new ArrayDeque<>();
37+
List<Integer> result = new ArrayList<>();
38+
for (int i = 1; i <= order.length; i++) {
39+
// Add i to the queue
40+
queue.offer(i);
41+
}
42+
// Now that we have built the queue, we can process getting peeps out
43+
while (!queue.isEmpty()) {
44+
// While we still have people in the queue
45+
int index = queue.poll();
46+
int amount = order[index - 1];
47+
// Let us process this person
48+
49+
if (amount <= x) {
50+
// this person is cleared to go
51+
result.add(index);
52+
53+
} else {
54+
// The amount is greater
55+
order[index - 1] = amount - x;
56+
// Add this person back to the queue
57+
queue.offer(index);
58+
}
59+
}
60+
return result;
61+
62+
}
63+
}
64+
65+
66+
// import java.util.*;
67+
// import java.io.*;
68+
// public class Solution {
69+
// public static void main(String[] args) {
70+
71+
// }
72+
// }
73+
74+

0 commit comments

Comments
 (0)