MCPcopy Create free account
hub / github.com/Ayush7614/Daily-Coding-DS-ALGO-Practice / getSkyline

Method getSkyline

Leetcode/Java/SkyLine.java:6–38  ·  view source on GitHub ↗
(int[][] buildings)

Source from the content-addressed store, hash-verified

4
5class Solution{
6 public List<List<Integer>> getSkyline(int[][] buildings)
7 {
8 List<List<Integer>> result=new ArrayList<>();
9 List<int[]> heights=new ArrayList<>();
10 for(int [] b:buildings)
11 {
12 heights.add(new int[]{b[0],-b[2]});
13 heights.add(new int[]{b[1],b[2]});
14 }
15 Collections.sort(heights,(a,b)->{
16 if(a[0]!=b[0]) return a[0]-b[0];
17 else return a[1]-b[1];
18 });
19 PriorityQueue<Integer> pq=new PriorityQueue<>((a,b)-> b-a);
20 pq.offer(0);
21 int prev=0;
22 for(int[] h:heights)
23 {
24 if(h[1]<0) pq.offer(-h[1]);
25 else pq.remove(h[1]);
26 int curr=pq.peek();
27 if(prev!=curr)
28 {
29 List<Integer> tmp=new ArrayList<>();
30 tmp.add(h[0]);
31 tmp.add(curr);
32 result.add(new ArrayList<>(tmp));
33 prev=curr;
34 }
35
36 }
37 return result;
38 }
39 public static void main()
40 {
41 int buildings={{0,2,3},{2,5,3}};

Callers 1

mainMethod · 0.95

Calls 4

addMethod · 0.80
sortMethod · 0.45
removeMethod · 0.45
peekMethod · 0.45

Tested by

no test coverage detected