Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4d72192

Browse files
Create Buy the ticket
1 parent cbefce6 commit 4d72192

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

‎Priority Queues/Buy the ticket

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include<queue>
2+
int buyTicket(int *arr, int n, int k) {
3+
queue<int> q;
4+
priority_queue<int> maxHeap;
5+
for(int i=0;i<n;i++){
6+
q.push(i);
7+
maxHeap.push(arr[i]);
8+
}
9+
int time=0;
10+
while(arr[q.front()]!=maxHeap.top()||q.front()!=k){
11+
if(arr[q.front()]==maxHeap.top()){
12+
q.pop();
13+
maxHeap.pop();
14+
time++;
15+
}
16+
else{
17+
int temp=q.front();
18+
q.pop();
19+
q.push(temp);
20+
}
21+
}
22+
return time+1;
23+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /