g0201_0300.s0218_the_skyline_problem.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0201_0300.s0218_the_skyline_problem;
// #Hard #Top_Interview_Questions #Array #Heap_Priority_Queue #Ordered_Set #Divide_and_Conquer
// #Segment_Tree #Binary_Indexed_Tree #Line_Sweep
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TreeMap;
public class Solution {
public List> getSkyline(int[][] buildings) {
List> list = new ArrayList<>();
List lines = new ArrayList<>();
for (int[] building : buildings) {
lines.add(new int[] {building[0], building[2]});
lines.add(new int[] {building[1], -building[2]});
}
lines.sort((a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]);
TreeMap map = new TreeMap<>();
map.put(0, 1);
int prev = 0;
for (int[] line : lines) {
if (line[1] > 0) {
map.put(line[1], map.getOrDefault(line[1], 0) + 1);
} else {
int f = map.get(-line[1]);
if (f == 1) {
map.remove(-line[1]);
} else {
map.put(-line[1], f - 1);
}
}
int curr = map.lastKey();
if (curr != prev) {
list.add(Arrays.asList(line[0], curr));
prev = curr;
}
}
return list;
}
}