You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: content/3_Silver/Priority_Queues.mdx
+9-6
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,7 @@ title: 'Priority Queues'
4
4
author: Darren Yao, Benjamin Qi, Jeffrey Hu, Shreyas Thumathy, Nathan Gong
5
5
prerequisites:
6
6
- greedy-sorting
7
+
- sorting-custom
7
8
description: 'A data structure that supports insert, query max, and pop max.'
8
9
frequency: 2
9
10
---
@@ -135,6 +136,8 @@ we iterate through the customers.
135
136
136
137
<CPPSection>
137
138
139
+
Implementation Note: Similarly to what was done in an [earlier module](/silver/sorting-custom?lang=cpp#sorting-in-descending-order), we can use `greater<>()` to create a min-heap instead of a max-heap.
140
+
138
141
```cpp
139
142
#include<algorithm>
140
143
#include<iostream>
@@ -156,19 +159,19 @@ int main() {
156
159
sort(v.begin(), v.end());
157
160
158
161
int last_room = 0;
159
-
priority_queue<pair<int,int>> pq; // min heap to store departure times.
162
+
using Cust = pair<int,int>;
163
+
priority_queue<Cust,vector<Cust>,greater<Cust>>
164
+
pq;//minheaptostoredeparturetimes.
160
165
for(inti=0;i<N;i++){
161
-
if (pq.empty() ||-pq.top().first>=v[i].first.first) {
166
+
if (pq.empty() ||pq.top().first>=v[i].first.first) {
162
167
last_room++;
163
-
// make the departure time negative so that we create a min heap
164
-
// (cleanest way to do a min heap... default is max in c++)
0 commit comments