-
Notifications
You must be signed in to change notification settings - Fork 13
/
Skyline Problem
30 lines (29 loc) · 1.02 KB
/
Skyline Problem
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public List<List<Integer>> getSkyline(int[][] buildings) {
List<List<Integer>> list = new ArrayList<>();
List<int[]> lines = new ArrayList<>();
for (int[] building: buildings) {
lines.add(new int[] {building[0], building[2]});
lines.add(new int[] {building[1], -building[2]});
}
Collections.sort(lines, (a, b)->a[0]==b[0]?b[1]-a[1]:a[0]-b[0]);
TreeMap<Integer, Integer> 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;
}
}