vector<vector<int>> getSkyline(vector<vector<int>>& buildings) {
set<int> edges;
for (auto& b : buildings) { edges.insert(b[0]); edges.insert(b[1]); }
vector<int> xs(edges.begin(), edges.end());
vector<vector<int>> result;
int prev = 0;
for (int x : xs) {
int height = 0;
for (auto& b : buildings) {
if (b[0] <= x && x < b[1]) height = max(height, b[2]);
}
if (height != prev) { result.push_back({x, height}); prev = height; }
}
return result;
}
public List<List<Integer>> getSkyline(int[][] buildings) {
TreeSet<Integer> edges = new TreeSet<>();
for (int[] b : buildings) { edges.add(b[0]); edges.add(b[1]); }
List<Integer> xs = new ArrayList<>(edges);
List<List<Integer>> result = new ArrayList<>();
int prev = 0;
for (int x : xs) {
int height = 0;
for (int[] b : buildings) {
if (b[0] <= x && x < b[1]) height = Math.max(height, b[2]);
}
if (height != prev) { result.add(Arrays.asList(x, height)); prev = height; }
}
return result;
}
def get_skyline(buildings):
xs = sorted({v for l, r, h in buildings for v in (l, r)})
result = []
prev = 0
for x in xs:
height = 0
for l, r, h in buildings:
if l <= x < r: height = max(height, h)
if height != prev:
result.append([x, height]); prev = height
return result
function getSkyline(buildings: number[][]): number[][] {
const xs = [...new Set(buildings.flatMap(([l, r]) => [l, r]))].sort((a, b) => a - b);
const result: number[][] = [];
let prev = 0;
for (const x of xs) {
let height = 0;
for (const [l, r, h] of buildings) {
if (l <= x && x < r) height = Math.max(height, h);
}
if (height !== prev) { result.push([x, height]); prev = height; }
}
return result;
}
vector<vector<int>> getSkyline(vector<vector<int>>& buildings) {
vector<pair<int, int>> events;
for (auto& b : buildings) {
events.push_back({b[0], -b[2]});
events.push_back({b[1], b[2]});
}
sort(events.begin(), events.end());
vector<int> active;
vector<vector<int>> result;
int prev = 0;
for (auto& [x, h] : events) {
if (h < 0) active.push_back(-h);
else active.erase(find(active.begin(), active.end(), h));
int cur = active.empty() ? 0 : *max_element(active.begin(), active.end());
if (cur != prev) { result.push_back({x, cur}); prev = cur; }
}
return result;
}
public List<List<Integer>> getSkyline(int[][] buildings) {
List<int[]> events = new ArrayList<>();
for (int[] b : buildings) {
events.add(new int[]{b[0], -b[2]});
events.add(new int[]{b[1], b[2]});
}
events.sort((a, b) -> a[0] != b[0] ? Integer.compare(a[0], b[0]) : Integer.compare(a[1], b[1]));
List<Integer> active = new ArrayList<>();
List<List<Integer>> result = new ArrayList<>();
int prev = 0;
for (int[] e : events) {
int x = e[0], h = e[1];
if (h < 0) active.add(-h);
else active.remove(Integer.valueOf(h));
int cur = active.isEmpty() ? 0 : Collections.max(active);
if (cur != prev) { result.add(Arrays.asList(x, cur)); prev = cur; }
}
return result;
}
def get_skyline(buildings):
events = []
for l, r, h in buildings:
events.append((l, -h))
events.append((r, h))
events.sort()
active = []
result = []
prev = 0
for x, h in events:
if h < 0: active.append(-h)
else: active.remove(h)
cur = max(active) if active else 0
if cur != prev:
result.append([x, cur]); prev = cur
return result
function getSkyline(buildings: number[][]): number[][] {
const events: number[][] = [];
for (const [l, r, h] of buildings) {
events.push([l, -h]);
events.push([r, h]);
}
events.sort((a, b) => a[0] - b[0] || a[1] - b[1]);
const active: number[] = [];
const result: number[][] = [];
let prev = 0;
for (const [x, h] of events) {
if (h < 0) active.push(-h);
else active.splice(active.indexOf(h), 1);
const cur = active.length === 0 ? 0 : Math.max(...active);
if (cur !== prev) { result.push([x, cur]); prev = cur; }
}
return result;
}
vector<vector<int>> getSkyline(vector<vector<int>>& buildings) {
set<int> edges;
for (auto& b : buildings) { edges.insert(b[0]); edges.insert(b[1]); }
vector<int> xs(edges.begin(), edges.end());
priority_queue<pair<int, int>> heap; // (height, right edge)
vector<vector<int>> result;
size_t i = 0; int prev = 0;
for (int x : xs) {
while (i < buildings.size() && buildings[i][0] <= x) {
heap.push({buildings[i][2], buildings[i][1]});
i++;
}
while (!heap.empty() && heap.top().second <= x) heap.pop();
int height = heap.empty() ? 0 : heap.top().first;
if (height != prev) { result.push_back({x, height}); prev = height; }
}
return result;
}
public List<List<Integer>> getSkyline(int[][] buildings) {
TreeSet<Integer> edges = new TreeSet<>();
for (int[] b : buildings) { edges.add(b[0]); edges.add(b[1]); }
List<Integer> xs = new ArrayList<>(edges);
PriorityQueue<int[]> heap = new PriorityQueue<>((a, b) -> b[0] - a[0]); // {height, right edge}
List<List<Integer>> result = new ArrayList<>();
int i = 0, prev = 0;
for (int x : xs) {
while (i < buildings.length && buildings[i][0] <= x) {
heap.add(new int[]{buildings[i][2], buildings[i][1]});
i++;
}
while (!heap.isEmpty() && heap.peek()[1] <= x) heap.poll();
int height = heap.isEmpty() ? 0 : heap.peek()[0];
if (height != prev) { result.add(Arrays.asList(x, height)); prev = height; }
}
return result;
}
def get_skyline(buildings):
xs = sorted({v for l, r, h in buildings for v in (l, r)})
heap = [] # (-height, right edge)
result = []
i = prev = 0
for x in xs:
while i < len(buildings) and buildings[i][0] <= x:
heapq.heappush(heap, (-buildings[i][2], buildings[i][1]))
i += 1
while heap and heap[0][1] <= x: heapq.heappop(heap)
height = -heap[0][0] if heap else 0
if height != prev:
result.append([x, height]); prev = height
return result
function getSkyline(buildings: number[][]): number[][] {
const xs = [...new Set(buildings.flatMap(([l, r]) => [l, r]))].sort((a, b) => a - b);
const heap = new Heap<number[]>((a, b) => a[0] > b[0]);
const result: number[][] = [];
let i = 0, prev = 0;
for (const x of xs) {
while (i < buildings.length && buildings[i][0] <= x) {
heap.push([buildings[i][2], buildings[i][1]]);
i++;
}
while (heap.size > 0 && heap.peek()[1] <= x) heap.pop();
const height = heap.size === 0 ? 0 : heap.peek()[0];
if (height !== prev) { result.push([x, height]); prev = height; }
}
return result;
}
class Heap<T> {
private a: T[] = [];
constructor(private before: (x: T, y: T) => boolean) {}
get size() { return this.a.length; }
peek(): T { return this.a[0]; }
push(v: T) {
const a = this.a;
a.push(v);
let i = a.length - 1;
while (i > 0) {
const p = (i - 1) >> 1;
if (!this.before(a[i], a[p])) break;
[a[i], a[p]] = [a[p], a[i]];
i = p;
}
}
pop(): T {
const a = this.a;
const top = a[0];
const last = a.pop()!;
if (a.length) {
a[0] = last;
let i = 0;
for (;;) {
let m = i;
const l = 2 * i + 1, r = l + 1;
if (l < a.length && this.before(a[l], a[m])) m = l;
if (r < a.length && this.before(a[r], a[m])) m = r;
if (m === i) break;
[a[i], a[m]] = [a[m], a[i]];
i = m;
}
}
return top;
}
}
vector<vector<int>> getSkyline(vector<vector<int>>& buildings) {
function<vector<vector<int>>(const vector<vector<int>>&, const vector<vector<int>>&)> merge =
[](const vector<vector<int>>& a, const vector<vector<int>>& b) {
vector<vector<int>> out;
size_t i = 0, j = 0; int h1 = 0, h2 = 0;
while (i < a.size() || j < b.size()) {
long long xa = i < a.size() ? a[i][0] : LLONG_MAX;
long long xb = j < b.size() ? b[j][0] : LLONG_MAX;
long long x = min(xa, xb);
if (xa == x) h1 = a[i++][1];
if (xb == x) h2 = b[j++][1];
int h = max(h1, h2);
if (out.empty() || out.back()[1] != h) out.push_back({(int)x, h});
}
return out;
};
function<vector<vector<int>>(int, int)> solve = [&](int lo, int hi) -> vector<vector<int>> {
if (lo == hi) return {{buildings[lo][0], buildings[lo][2]}, {buildings[lo][1], 0}};
int mid = (lo + hi) >> 1;
return merge(solve(lo, mid), solve(mid + 1, hi));
};
return buildings.empty() ? vector<vector<int>>{} : solve(0, (int)buildings.size() - 1);
}
public List<List<Integer>> getSkyline(int[][] buildings) {
return buildings.length == 0 ? new ArrayList<>() : solve(buildings, 0, buildings.length - 1);
}
private List<List<Integer>> merge(List<List<Integer>> a, List<List<Integer>> b) {
List<List<Integer>> out = new ArrayList<>();
int i = 0, j = 0, h1 = 0, h2 = 0;
while (i < a.size() || j < b.size()) {
long xa = i < a.size() ? a.get(i).get(0) : Long.MAX_VALUE;
long xb = j < b.size() ? b.get(j).get(0) : Long.MAX_VALUE;
long x = Math.min(xa, xb);
if (xa == x) h1 = a.get(i++).get(1);
if (xb == x) h2 = b.get(j++).get(1);
int h = Math.max(h1, h2);
if (out.isEmpty() || out.get(out.size() - 1).get(1) != h) out.add(Arrays.asList((int) x, h));
}
return out;
}
private List<List<Integer>> solve(int[][] buildings, int lo, int hi) {
if (lo == hi) return Arrays.asList(Arrays.asList(buildings[lo][0], buildings[lo][2]), Arrays.asList(buildings[lo][1], 0));
int mid = (lo + hi) >> 1;
return merge(solve(buildings, lo, mid), solve(buildings, mid + 1, hi));
}
def get_skyline(buildings):
def merge(a, b):
out = []
i = j = h1 = h2 = 0
while i < len(a) or j < len(b):
xa = a[i][0] if i < len(a) else float('inf')
xb = b[j][0] if j < len(b) else float('inf')
x = min(xa, xb)
if xa == x: h1 = a[i][1]; i += 1
if xb == x: h2 = b[j][1]; j += 1
h = max(h1, h2)
if not out or out[-1][1] != h: out.append([x, h])
return out
def solve(lo, hi):
if lo == hi: return [[buildings[lo][0], buildings[lo][2]], [buildings[lo][1], 0]]
mid = (lo + hi) >> 1
return merge(solve(lo, mid), solve(mid + 1, hi))
return [] if not buildings else solve(0, len(buildings) - 1)
function getSkyline(buildings: number[][]): number[][] {
const merge = (a: number[][], b: number[][]): number[][] => {
const out: number[][] = [];
let i = 0, j = 0, h1 = 0, h2 = 0;
while (i < a.length || j < b.length) {
const xa = i < a.length ? a[i][0] : Infinity;
const xb = j < b.length ? b[j][0] : Infinity;
const x = Math.min(xa, xb);
if (xa === x) h1 = a[i++][1];
if (xb === x) h2 = b[j++][1];
const h = Math.max(h1, h2);
if (out.length === 0 || out[out.length - 1][1] !== h) out.push([x, h]);
}
return out;
};
const solve = (lo: number, hi: number): number[][] => {
if (lo === hi) return [[buildings[lo][0], buildings[lo][2]], [buildings[lo][1], 0]];
const mid = (lo + hi) >> 1;
return merge(solve(lo, mid), solve(mid + 1, hi));
};
return buildings.length === 0 ? [] : solve(0, buildings.length - 1);
}