@@ -46,15 +46,13 @@ public List<int[]> getSkyline(int[][] buildings) {
|
46 | 46 | List<int[]> res = new ArrayList<>();
|
47 | 47 | int N = buildings.length;
|
48 | 48 | if (N == 0) return res;
|
49 |
| - |
| 49 | +
|
50 | 50 | // xi, hi, L/R
|
51 |
| -int[][] verts = new int[N * 2][4]; |
| 51 | +int[][] verts = new int[N * 2][3]; |
52 | 52 | int t = 0;
|
53 |
| -int f = 0; |
54 | 53 | for (int[] b: buildings) {
|
55 |
| -verts[t++] = new int[]{b[0], b[2], L, f}; |
56 |
| -verts[t++] = new int[]{b[1], b[2], R, f}; |
57 |
| -f++; |
| 54 | +verts[t++] = new int[]{b[0], b[2], L}; |
| 55 | +verts[t++] = new int[]{b[1], b[2], R}; |
58 | 56 | }
|
59 | 57 | Comparator<int[]> comp1 = new Comparator<int[]>() {
|
60 | 58 | @Override
|
@@ -72,45 +70,29 @@ public int compare(int[] v1, int[] v2) {
|
72 | 70 | }
|
73 | 71 | };
|
74 | 72 | Arrays.sort(verts, comp1);
|
75 |
| - |
76 |
| -Comparator<int[]> comp2 = (v1, v2) -> Integer.compare(v2[1], v1[1]); |
77 |
| -List<int[]> hs = new ArrayList<>(); |
| 73 | +
|
| 74 | +Comparator<Integer> comp2 = (h1, h2) -> Integer.compare(h2, h1); |
| 75 | +PriorityQueue<Integer> pq = new PriorityQueue<>(comp2); |
78 | 76 | for (int[] vi: verts) {
|
79 | 77 | int xi = vi[0];
|
80 | 78 | int hi = vi[1];
|
81 | 79 | int Di = vi[2];
|
82 |
| -int flag = vi[3]; |
83 | 80 |
|
84 |
| -Collections.sort(hs, comp2); |
85 | 81 | if (Di == L) { // L
|
86 |
| -if (hs.isEmpty() || hs.get(0)[1] < hi) { |
| 82 | +if (pq.isEmpty() || pq.peek() < hi) { |
87 | 83 | res.add(new int[]{xi, hi});
|
88 | 84 | }
|
89 |
| -hs.add(vi); |
| 85 | +pq.add(hi); |
90 | 86 | } else { // R
|
91 |
| -int size = hs.size(); |
92 |
| -for (int i=0; i<size; i++) { |
93 |
| -if (hs.get(i)[3] == flag) { |
94 |
| -hs.remove(i); |
95 |
| -break; |
96 |
| -} |
97 |
| -} |
98 |
| -if (hs.isEmpty() || hs.get(0)[1] < hi) { |
99 |
| -int y = hs.isEmpty() ? 0 : hs.get(0)[1]; |
| 87 | +pq.remove(hi); |
| 88 | +if (pq.isEmpty() || pq.peek() < hi) { |
| 89 | +int y = pq.isEmpty() ? 0 : pq.peek(); |
100 | 90 | res.add(new int[]{xi, y});
|
101 | 91 | }
|
102 | 92 | }
|
103 | 93 | }
|
| 94 | + |
104 | 95 | return res;
|
105 | 96 | }
|
106 | 97 |
|
107 |
| - |
108 |
| - |
109 |
| - |
110 |
| - |
111 |
| - |
112 |
| - |
113 |
| - |
114 |
| - |
115 |
| - |
116 | 98 | }
|
0 commit comments