forked from jvm-coder/Hacktoberfest2022_aakash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsky.java
More file actions
28 lines (26 loc) · 922 Bytes
/
sky.java
File metadata and controls
28 lines (26 loc) · 922 Bytes
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
class Solution {
public List<List<Integer>> getSkyline(int[][] buildings){
int pt[][] = new int[buildings.length * 2][2];
int itr = 0;
for(var b:buildings){
pt[itr++] = new int[]{b[0],-b[2]};
pt[itr++] = new int[]{b[1],b[2]};
}
Arrays.sort(pt,(a,b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
List<List<Integer>> ans = new ArrayList<>();
PriorityQueue<Integer> ht = new PriorityQueue<>(Collections.reverseOrder());
ht.add(0);
int c_ht = 0;
for(int i = 0;i < pt.length;i++){
int x = pt[i][0];
int h = pt[i][1];
if(h < 0) ht.add(-h);
else ht.remove(h);
if(c_ht != ht.peek()){
ans.add(Arrays.asList(x,ht.peek()));
c_ht = ht.peek();
}
}
return ans;
}
}