Opening the reading…
Opening the reading…
HEAP (PRIORITY QUEUE) › IMPLEMENTARY 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 →Between two consecutive building edges, the set of buildings covering the ground does not change. Therefore the visible height is constant throughout that interval, and it is simply the largest height among the buildings currently covering the sweep position. The only x-coordinates worth examining are building left edges, where a height appears, and right edges, where a height disappears.
Sweep those edges from left to right while maintaining all heights that are active at the current x-coordinate. If the largest active height changes, the skyline changes there too, so you record the current x and the new maximum. A ground height of 0 stays in the data structure permanently; it makes the final drop to the ground an ordinary maximum change instead of a special case.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(n log n) | There are 2n events, and sorting them costs O(n log n). Each event performs one multiset insertion or deletion, costing O(log n), so the sweep also costs O(n log n); no event is processed more than once. |
| Space | O(n) extra | The event list uses O(n) space and the multiset contains at most n active building heights, while the result is required output and is excluded. The bound is still O(n) in the worst overlap shape, where many buildings are active simultaneously. |
#include <algorithm>
#include <set>
#include <utility>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int>> getSkyline(vector<vector<int>>& buildings) {
vector<pair<int, int>> events;
for (const vector<int>& building : buildings) {
int left = building[0];
int right = building[1];
int height = building[2];
events.push_back({left, -height});
events.push_back({right, height});
}
sort(events.begin(), events.end());
multiset<int> active;
active.insert(0);
vector<vector<int>> result;
int previousHeight = 0;
for (const auto& event : events) {
int x = event.first;
int encodedHeight = event.second;
if (encodedHeight < 0) {
active.insert(-encodedHeight);
} else {
active.erase(active.find(encodedHeight));
}
int currentHeight = *active.rbegin();
if (currentHeight != previousHeight) {
result.push_back({x, currentHeight});
previousHeight = currentHeight;
}
}
return result;
}
};The negative encoding does two jobs at once: it distinguishes starts from ends, and it gives same-position starts the desired order under ordinary pair sorting. The largest height starts first, while all starts come before ends. This prevents a temporary lower contour at one x-coordinate when several buildings meet there. The multiset stores duplicates, so two buildings with the same height remain independent.
A max-heap is a reasonable alternative when you want constant-time access to the candidate maximum. It cannot remove an arbitrary height efficiently, so an ending building is marked inactive and left in the heap. Before reading the maximum, repeatedly discard heap values whose active count is zero. This is a different arrangement rather than a better asymptotic bound: it uses O(n) extra space and has O(n log n) expected time because the counts use a hash table.
#include <algorithm>
#include <functional>
#include <queue>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int>> getSkyline(vector<vector<int>>& buildings) {
vector<pair<int, int>> events;
for (const vector<int>& building : buildings) {
events.push_back({building[0], -building[2]});
events.push_back({building[1], building[2]});
}
sort(events.begin(), events.end());
priority_queue<int> active;
unordered_map<int, int> count;
active.push(0);
count[0] = 1;
vector<vector<int>> result;
int previousHeight = 0;
for (const auto& event : events) {
int x = event.first;
int encodedHeight = event.second;
if (encodedHeight < 0) {
int height = -encodedHeight;
active.push(height);
++count[height];
} else {
--count[encodedHeight];
}
while (!active.empty() && count[active.top()] == 0) {
active.pop();
}
int currentHeight = active.top();
if (currentHeight != previousHeight) {
result.push_back({x, currentHeight});
previousHeight = currentHeight;
}
}
return result;
}
};