Default Behavior (Max-Heap)
If you declare `priority_queue<int> pq;`, it behaves as a Max-Heap. The element with the highest value will always be popped first.
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
The C++ Standard Template Library implementation of a Max-Heap. It automatically keeps the largest element at the top.
If you declare `priority_queue<int> pq;`, it behaves as a Max-Heap. The element with the highest value will always be popped first.
To make it a Min-Heap, you must pass a vector and a comparator: `priority_queue<int, vector<int>, greater<int>> pq;`. Now, the smallest element is popped first.
`push()` and `pop()` operate in O(log N) time, as the heap must re-balance itself. `top()` operates in O(1) time, instantly retrieving the highest priority element.