class Point {
int x;
int h;
public Point(int x, int h) {
this.x = x;
this.h = h;
}
}
class MyComparator implements Comparator<Point> {
@Override
public int compare(Point p1, Point p2) {
if (p1.x == p2.x) {
return p2.h - p1.h;
}
return p1.x - p2.x;
}
}
public List<int[]> getSkyline(int[][] buildings) {
List<int[]> res = new ArrayList<>();
List<Point> list = getSortedList(buildings);
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
maxHeap.offer(0);
for (Point p : list) {
int h = p.h;
int x = p.x;
if (h > 0) {
if (h > maxHeap.peek()) {
res.add(new int[] {x, h});
}
maxHeap.offer(h);
} else {
h = -h;
maxHeap.remove(h);
if (h > maxHeap.peek()) {
res.add(new int[] {x, maxHeap.peek()});
}
}
}
return res;
}
private List<Point> getSortedList(int[][] buildings) {
List<Point> list = new ArrayList<>();
for (int[] b : buildings) {
Point leftBoundary = new Point(b[0], b[2]);
Point rightBoundary = new Point(b[1], -b[2]);
list.add(leftBoundary);
list.add(rightBoundary);
}
Collections.sort(list, new MyComparator());
return list;
}