天际线问题
Posted Alice_yufeng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了天际线问题相关的知识,希望对你有一定的参考价值。
class Solution
public List<List<Integer>> getSkyline(int[][] buildings)
PriorityQueue<int[]> pq = new PriorityQueue<int[]>((a, b) -> b[1] - a[1]);
List<Integer> boundaries = new ArrayList<Integer>();
for (int[] building : buildings)
boundaries.add(building[0]);
boundaries.add(building[1]);
Collections.sort(boundaries);
List<List<Integer>> ret = new ArrayList<List<Integer>>();
int n = buildings.length, idx = 0;
for (int boundary : boundaries)
while (idx < n && buildings[idx][0] <= boundary)
pq.offer(new int[]buildings[idx][1], buildings[idx][2]);
idx++;
while (!pq.isEmpty() && pq.peek()[0] <= boundary)
pq.poll();
int maxn = pq.isEmpty() ? 0 : pq.peek()[1];
if (ret.size() == 0 || maxn != ret.get(ret.size() - 1).get(1))
ret.add(Arrays.asList(boundary, maxn));
return ret;
以上是关于天际线问题的主要内容,如果未能解决你的问题,请参考以下文章
[H扫描线] lc218. 天际线问题(扫描线求轮廓+边界情况+好题+算法技巧)
LeetCode - 807. 保持城市天际线 - Java