Opening the reading…
Opening the reading…
HEAP (PRIORITY QUEUE) › INTRODUCTORY QUESTIONS
Open — the attempt gate is not wired up yet
This editorial is meant to unlock after you have run the problem at least once, with the worked solution behind one further deliberate click. That needs per-learner unlock state nothing stores today, so for now the whole article is open.
Try it yourself first →A binary min-heap is an array with two simultaneous properties: every parent is no larger than either child, and the occupied positions form a complete tree from index 0 onward. For an element at index i, its parent is at (i - 1) / 2, while its children are at 2 * i + 1 and 2 * i + 2. Keeping the array compact is what makes the last element the natural replacement during extraction.
Insertion appends at the first unused position, so only the path toward the root can violate the ordering rule. Extraction removes the root, moves the last element into its position, and fixes the possible violation by moving downward. Indexed deletion looks different, but the requested value can be made the minimum by replacing it with a value smaller than every legal input; after that, one ordinary extraction removes exactly that position.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(q log q) | Each query performs at most one sift-up or sift-down, and a repair visits one path whose length is at most the heap height, O(log q). The total counts every query once and does not repeat work across unrelated heap branches. |
| Space | O(q) extra | The working heap can hold q values in the worst case, and the answer vector is required output and is excluded from the extra-space bound. The helper methods use O(1) additional space; the bound reaches O(q) when many insertions occur before removals. |
#include <algorithm>
#include <climits>
#include <vector>
using namespace std;
class Solution {
vector<int> heap;
void siftUp(int i) {
while (i > 0) {
int parent = (i - 1) / 2;
if (heap[parent] <= heap[i]) {
break;
}
swap(heap[parent], heap[i]);
i = parent;
}
}
void siftDown(int i) {
while (true) {
int left = 2 * i + 1;
int right = left + 1;
int smallest = i;
if (left < static_cast<int>(heap.size()) &&
heap[left] < heap[smallest]) {
smallest = left;
}
if (right < static_cast<int>(heap.size()) &&
heap[right] < heap[smallest]) {
smallest = right;
}
if (smallest == i) {
break;
}
swap(heap[i], heap[smallest]);
i = smallest;
}
}
int extractMinInternal() {
if (heap.empty()) {
return -1;
}
int answer = heap[0];
heap[0] = heap.back();
heap.pop_back();
if (!heap.empty()) {
siftDown(0);
}
return answer;
}
public:
vector<int> processQueries(vector<vector<int>>& queries) {
heap.clear();
vector<int> answer;
answer.reserve(queries.size());
for (const vector<int>& query : queries) {
int type = query[0];
if (type == 1) {
heap.push_back(query[1]);
siftUp(static_cast<int>(heap.size()) - 1);
} else if (type == 2) {
int index = query[1];
if (0 <= index && index < static_cast<int>(heap.size())) {
heap[index] = INT_MIN;
siftUp(index);
extractMinInternal();
}
} else {
answer.push_back(extractMinInternal());
}
}
return answer;
}
};The important placement is in the deletion branch: the sentinel is written at the requested index before siftUp runs. Once it reaches index 0, extractMinInternal removes it using exactly the same root replacement and sift-down logic as a type 3 query. Keeping one extraction helper prevents the two removal paths from drifting apart.
A valid deletion can also replace the target with the last heap value, shrink the vector, and repair the replacement in whichever direction is necessary. If the replacement is smaller than its parent, sift it upward; otherwise sift it downward. This is a different arrangement, not an asymptotic optimisation: both methods take O(log q) time and O(q) working space. The sentinel version buys a single, easily verified removal path.
#include <algorithm>
#include <vector>
using namespace std;
class Solution {
vector<int> heap;
void siftUp(int i) {
while (i > 0) {
int parent = (i - 1) / 2;
if (heap[parent] <= heap[i]) {
break;
}
swap(heap[parent], heap[i]);
i = parent;
}
}
void siftDown(int i) {
while (true) {
int left = 2 * i + 1;
int right = left + 1;
int smallest = i;
if (left < static_cast<int>(heap.size()) &&
heap[left] < heap[smallest]) {
smallest = left;
}
if (right < static_cast<int>(heap.size()) &&
heap[right] < heap[smallest]) {
smallest = right;
}
if (smallest == i) {
break;
}
swap(heap[i], heap[smallest]);
i = smallest;
}
}
void deleteAt(int index) {
int last = static_cast<int>(heap.size()) - 1;
if (index == last) {
heap.pop_back();
return;
}
heap[index] = heap.back();
heap.pop_back();
if (index > 0 && heap[index] < heap[(index - 1) / 2]) {
siftUp(index);
} else {
siftDown(index);
}
}
int extractMinInternal() {
if (heap.empty()) {
return -1;
}
int answer = heap[0];
heap[0] = heap.back();
heap.pop_back();
if (!heap.empty()) {
siftDown(0);
}
return answer;
}
public:
vector<int> processQueries(vector<vector<int>>& queries) {
heap.clear();
vector<int> answer;
answer.reserve(queries.size());
for (const vector<int>& query : queries) {
if (query[0] == 1) {
heap.push_back(query[1]);
siftUp(static_cast<int>(heap.size()) - 1);
} else if (query[0] == 2) {
int index = query[1];
if (0 <= index && index < static_cast<int>(heap.size())) {
deleteAt(index);
}
} else {
answer.push_back(extractMinInternal());
}
}
return answer;
}
};