File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java.util.*;
2
+ public class Solution {
3
+
4
+ public static int buyTicket(int input[], int k) {
5
+ /* Your class should be named Solution
6
+ * Don't write main().
7
+ * Don't read input, it is passed as function argument.
8
+ * Return output and don't print it.
9
+ * Taking input and printing output is handled automatically.
10
+ */
11
+ Queue<Integer> queue = new LinkedList<>();
12
+ PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
13
+ for (int i=0;i<input.length;i++){
14
+ queue.add(i);
15
+ pq.add(input[i]);
16
+ }
17
+ int time=0;
18
+ while (!queue.isEmpty()){
19
+ if (input[queue.peek()] < pq.peek())
20
+ {
21
+ queue.add(queue.poll());
22
+ }
23
+ else
24
+ {
25
+ int idx = queue.poll();
26
+ pq.remove();
27
+ time++;
28
+ if (idx == k)
29
+ {
30
+ break;
31
+ }
32
+ }
33
+ }
34
+ return time;
35
+
36
+ }
37
+ }
You can’t perform that action at this time.
0 commit comments