Skip to content

Commit e5cd6fe

Browse files
committed
pq: don't negate elements in module sol
1 parent 85122c4 commit e5cd6fe

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

content/3_Silver/Priority_Queues.mdx

+9-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ title: 'Priority Queues'
44
author: Darren Yao, Benjamin Qi, Jeffrey Hu, Shreyas Thumathy, Nathan Gong
55
prerequisites:
66
- greedy-sorting
7+
- sorting-custom
78
description: 'A data structure that supports insert, query max, and pop max.'
89
frequency: 2
910
---
@@ -135,6 +136,8 @@ we iterate through the customers.
135136

136137
<CPPSection>
137138

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+
138141
```cpp
139142
#include <algorithm>
140143
#include <iostream>
@@ -156,19 +159,19 @@ int main() {
156159
sort(v.begin(), v.end());
157160

158161
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; // min heap to store departure times.
160165
for (int i = 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) {
162167
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++)
165-
pq.push(make_pair(-v[i].first.second, last_room));
168+
pq.push(make_pair(v[i].first.second, last_room));
166169
ans[v[i].second] = last_room;
167170
} else {
168171
// accessing the minimum departure time
169172
pair<int, int> minimum = pq.top();
170173
pq.pop();
171-
pq.push(make_pair(-v[i].first.second, minimum.second));
174+
pq.push(make_pair(v[i].first.second, minimum.second));
172175
ans[v[i].second] = minimum.second;
173176
}
174177
}

content/3_Silver/Sorting_Custom.mdx

+18-4
Original file line numberDiff line numberDiff line change
@@ -433,11 +433,25 @@ for an explanation of how `cmp_to_key` works.
433433

434434
### Sorting in Descending Order
435435

436-
Here are some alternatives to simply sorting the array and then reversing it:
437436
<LanguageSection>
438437

439438
<CPPSection>
440439

440+
There are many ways to do this.
441+
442+
Sorting and then reversing:
443+
444+
```
445+
sort(begin(edges), end(edges));
446+
reverse(begin(edges), end(edges));
447+
```
448+
449+
Sorting using reverse iterators:
450+
451+
```
452+
sort(rbegin(edges), rend(edges));
453+
```
454+
441455
For data structures (such as pairs and arrays) where `operator>` is defined:
442456

443457
```
@@ -457,13 +471,13 @@ We can replace all occurrences of `Integer.compare(x, y)` with
457471

458472
<PySection>
459473

474+
In Python, we can pass the parameter
475+
`reverse=True` to the `sort` or `sorted` function.
476+
460477
</PySection>
461478

462479
</LanguageSection>
463480

464-
In Python, we can pass the parameter
465-
`reverse=True` to the `sort` or `sorted` function.
466-
467481
### Sorting by Multiple Criteria
468482

469483
Now, suppose we wanted to sort a list of `Edge`s in ascending order, primarily

0 commit comments

Comments
 (0)