Skip to content

Commit fdf775b

Browse files
authored
Create Buy the Ticket
1 parent 65fbd29 commit fdf775b

File tree

1 file changed

+43
-0
lines changed
  • Course 2 - Data Structures in JAVA/Lecture 21 - Priority Queues II

1 file changed

+43
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
You want to buy a ticket for a well-known concert which is happening in your city. But the number of tickets available is limited.
3+
Hence the sponsors of the concert decided to sell tickets to customers based on some priority.
4+
A queue is maintained for buying the tickets and every person is attached with a priority (an integer, 1 being the lowest priority).
5+
The tickets are sold in the following manner -
6+
1. The first person (pi) in the queue requests for the ticket.
7+
2. If there is another person present in the queue who has higher priority than pi, then ask pi to move at end of the queue without giving him the ticket.
8+
3. Otherwise, give him the ticket (and don't make him stand in queue again).
9+
Giving a ticket to a person takes exactly 1 minute and it takes no time for removing and adding a person to the queue. And you can assume that no new person joins the queue.
10+
Given a list of priorities of N persons standing in the queue and the index of your priority (indexing starts from 0).
11+
Find and return the time it will take until you get the ticket.
12+
13+
Input Format:
14+
The first line of input contains an integer, that denotes the value of total number of people standing in queue or the size of the array of priorities. Let us denote it with the symbol N.
15+
The following line contains N space separated integers, that denote the value of the elements of the array of priorities.
16+
The following contains an integer, that denotes the value of index of your priority. Let us denote it with symbol k.
17+
18+
Output Format :
19+
The first and only line of output contains the time required for you to get the ticket.
20+
21+
Constraints:
22+
Time Limit: 1 sec
23+
24+
Sample Input 1 :
25+
3
26+
3 9 4
27+
2
28+
Sample Output 1 :
29+
2
30+
Sample Output 1 Explanation :
31+
Person with priority 3 comes out. But there is a person with higher priority than him. So he goes and then stands in the queue at the end.
32+
Queue's status : {9, 4, 3}. Time : 0 secs.
33+
Next, the person with priority 9 comes out. And there is no person with higher priority than him. So he'll get the ticket. Queue's status : {4, 3}. Time : 1 secs.
34+
Next, the person with priority 4 comes out (which is you). And there is no person with higher priority than you. So you'll get the ticket. Time : 2 secs.
35+
36+
Sample Input 2 :
37+
5
38+
2 3 2 2 4
39+
3
40+
Sample Output 2 :
41+
4
42+
*/
43+

0 commit comments

Comments
 (0)