Skip to content

Commit 82d4467

Browse files
Add files via upload
1 parent 45a07b0 commit 82d4467

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

BUY THE TICKET.txt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
}

0 commit comments

Comments
 (0)