From 74a80228cd323f10c23da9f461f484ca92a45f18 Mon Sep 17 00:00:00 2001 From: Shagufta Shahroz <60958363+ShaguftaShahroz@users.noreply.github.com> Date: Mon, 13 Jun 2022 19:14:19 +0530 Subject: [PATCH] The code wasn't correct I have made the necessary changes....please have a look and approve it:) --- Priority Queues:Buy the ticket | 43 +++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/Priority Queues:Buy the ticket b/Priority Queues:Buy the ticket index 344bf19..d5f3f9a 100644 --- a/Priority Queues:Buy the ticket +++ b/Priority Queues:Buy the ticket @@ -4,23 +4,38 @@ import java.util.PriorityQueue; public class Solution { public static int buyTicket(int input[], int k) { - PriorityQueue pq = new PriorityQueue<>(Collections.reverseOrder()); - - for(int i=0;i q = new LinkedList(); + PriorityQueue pq = new PriorityQueue(10,Collections.reverseOrder()); + for(int i: input){ + q.add(i); + pq.add(i); } - - int ans = 0; + int count =0; while(!pq.isEmpty()){ - int x = pq.element(); - if(x == input[k]){ - break; - }else{ - pq.remove(); - ans++; + if(q.peek().equals(pq.peek())){ + if(k==0){ + return count+1; + } + else + { + count++; + q.poll(); + pq.poll(); + k--; + } + } + else { + q.add(q.peek()); + q.poll(); + if(k==0){ + k = q.size()-1; + + } + else { + k--; + } } } - - return ans + 1; + return count; } }