博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode The Skyline Problem
阅读量:5063 次
发布时间:2019-06-12

本文共 3578 字,大约阅读时间需要 11 分钟。

原题链接在这里:

题目:

A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a program to output the skyline formed by these buildings collectively (Figure B).

The geometric information of each building is represented by a triplet of integers [Li, Ri, Hi], where Li and Ri are the x coordinates of the left and right edge of the ith building, respectively, and Hi is its height. It is guaranteed that 0 ≤ Li, Ri ≤ INT_MAX0 < Hi ≤ INT_MAX, and Ri - Li > 0. You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0.

For instance, the dimensions of all buildings in Figure A are recorded as: [ [2 9 10], [3 7 15], [5 12 12], [15 20 10], [19 24 8] ] .

The output is a list of "key points" (red dots in Figure B) in the format of [ [x1,y1], [x2, y2], [x3, y3], ... ] that uniquely defines a skyline. A key point is the left endpoint of a horizontal line segment. Note that the last key point, where the rightmost building ends, is merely used to mark the termination of the skyline, and always has zero height. Also, the ground in between any two adjacent buildings should be considered part of the skyline contour.

For instance, the skyline in Figure B should be represented as:[ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ].

Notes:

  • The number of buildings in any input list is guaranteed to be in the range [0, 10000].
  • The input list is already sorted in ascending order by the left x position Li.
  • The output list must be sorted by the x position.
  • There must be no consecutive horizontal lines of equal height in the output skyline. For instance, [...[2 3], [4 5], [7 5], [11 5], [12 7]...] is not acceptable; the three lines of height 5 should be merged into one in the final output as such: [...[2 3], [4 5], [12 7], ...]

题解:

发现result里的点都是高度出现落差时出现的。

把input 拆成左节点横坐标与高,右节点很坐标与高,为了区分,右节点的高设成负值。都插入verti List里然后排序。排序的Comparator 是先按照横坐标从小到大排序,若是横坐标相同,按照高度从大到下排序,因为若是两个building相连,第二个比第一个高, res只保存一个点就是高building的左上角.

从前往后扫verti, 遇到左边就把其高度存入大堆里,更新cur为最大高,遇到右边就把对应左边拿出,更新cur = maxHeap.isEmpty() ? 0 : maxHeap.peek().

当前最大高度cur 与之前最大高度 pre不同时,就吧{temp[0], cur} 存入res中去。

Time Complexity: O(nlogn). Space: O(n).

AC Java:

1 public class Solution { 2     public List
getSkyline(int[][] buildings) { 3 List
res = new ArrayList
(); 4 if(buildings == null || buildings.length == 0 || buildings[0].length != 3){ 5 return res; 6 } 7 8 PriorityQueue
maxHeap = new PriorityQueue
(Collections.reverseOrder()); 9 List
verti = new ArrayList
();10 for(int [] build : buildings){11 verti.add(new int[]{build[0], build[2]}); //左边界12 verti.add(new int[]{build[1], -build[2]}); //右边界,高度存成负数以便区分13 }14 Collections.sort(verti, new Comparator
(){15 public int compare(int [] a1, int[] a2){16 if(a1[0] == a2[0]){17 return a2[1] - a1[1];18 }19 return a1[0] - a2[0];20 }21 });22 23 int pre = 0; //之前最大高度24 int cur = 0; //当前最大高度25 for(int [] build : verti){26 if(build[1] > 0){ //左边界27 maxHeap.add(build[1]);28 cur = maxHeap.peek();29 }else{ //右边界30 maxHeap.remove(-build[1]);31 cur = maxHeap.isEmpty() ? 0 : maxHeap.peek();32 }33 34 if(cur != pre){ //出现高度差35 res.add(new int[]{build[0], cur});36 pre = cur;37 }38 }39 return res;40 }41 }

 

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/4944267.html

你可能感兴趣的文章
oracle导出/导入 expdp/impdp
查看>>
Objective - C基础: 第四天 - 10.SEL类型的基本认识
查看>>
Android TextView加上阴影效果
查看>>
OA项目设计的能力③
查看>>
《梦断代码》读书笔记(三)
查看>>
Java8 Lambda表达应用 -- 单线程游戏server+异步数据库操作
查看>>
[Unity3D]Unity3D游戏开发MatchTarget的作用攀登效果实现
查看>>
AngularJS学习篇(一)
查看>>
关于Xshell无法连接centos6.4的问题
查看>>
css3动画——基本准则
查看>>
输入月份和日期,得出是今年第几天
查看>>
pig自定义UDF
查看>>
Kubernetes 运维学习笔记
查看>>
spring security 11种过滤器介绍
查看>>
代码实现导航栏分割线
查看>>
大数据学习系列(8)-- WordCount+Block+Split+Shuffle+Map+Reduce技术详解
查看>>
【AS3代码】播放FLV视频流的三步骤!
查看>>
枚举的使用
查看>>
luogu4849 寻找宝藏 (cdq分治+dp)
查看>>
日志框架--(一)基础篇
查看>>