917. Implement PQ using Heaps

Implement a Priority Queue using heaps. It should have the following methods:

  • PriorityQueue() initializes the PriorityQueue object
  • push(val) inserts val into the priority queue
  • pop() removes the element with the highest value
  • top() returns the highest value element without removing it from the queue
  • empty() returns 1 if the queue is empty else 0.

Given input will always be valid.

Example 1:

Input: [push(10), push(5), top(), pop(), top(), empty()]

Output: [null, null, 10, null, 5, 0]

Explanation:

push(10): pushes 10 into the queue

push(5): pushes 5 into the queue

top(): returns 10 as the highest element in the queue

pop(): removes 10

top(): returns highest 5 as the highest element in queue

empty(): returns 0 as queue is not empty.

Example 2:

Input: [empty(), push(4), pop(), empty(), push(5), top()]

Output: [1, null, null, 1, null, 5]

Explanation:

empty(): returns 1 as queue is empty

push(4): pushes 4 into the queue

pop(): removes 4

empty(): returns 1 as queue is empty

push(5): pushes 5 into the queue

top(): returns 5 as the highest element in the queue

Now Your Turn!

Pick the correct output for the given input

Input: [push(10), top(), top(), pop(), empty()]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= Number of instructions <= 105
  • 0 <= val in push(val) <= 1000
  • All input will be valid

Fun Facts

0
class PriorityQueue {
public:
PriorityQueue() {
 
}
 
void push(int val) {
 
}
 
void pop() {
 
}
 
int top() {
 
}
bool empty(){
 
}
};
Test Case

Input:

Nums