Priority Queue (Heap)
C++
Medium
4 views
Problem Description
Queue where highest priority element comes first.
Real Life: Emergency room - critical patients treated first.
Step-by-Step Logic:
1. Create priority_queue
2. Push elements
3. Top always gives largest element
4. Pop removes largest
5. Automatically maintains order
Official Solution
void stl_q8_priority_queue() {
priority_queue<int> pq;
pq.push(30);
pq.push(10);
pq.push(50);
pq.push(20);
cout << "Priority Queue (max heap):" << endl;
while(!pq.empty()) {
cout << pq.top() << " ";
pq.pop();
}
cout << endl;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!