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).

解题思路:

本题如果一个矩形一个矩形的加入,涉及到回溯问题,比较复杂。

一个简便的思路是,根据横坐标排序,然后遍历求拐点。求拐点的时候用一个最大化heap来保存当前的楼顶高度,遇到左边节点,就在heap中插入高度信息,遇到右边节点就 从heap中删除高度。分别用pre与cur来表示之前的高度与当前的高度,当cur != pre的时候说明出现了拐点。JAVA实现如下:

	static public List<int[]> getSkyline(int[][] buildings) {
List<int[]> res = new ArrayList<int[]>();
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(11,
new Comparator<Integer>() {
public int compare(Integer a, Integer b) {
return b - a;
}
});
List<int[]> bl = new ArrayList<int[]>();
for (int i = 0; i < buildings.length; i++) {
bl.add(new int[] { buildings[i][0], buildings[i][2] });
bl.add(new int[] { buildings[i][1], -buildings[i][2] });
}
Collections.sort(bl, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
return a[0] == b[0] ? b[1] - a[1] : a[0] - b[0];
}
});
int pre = 0, cur = 0;
for (int i = 0; i < bl.size(); i++) {
int[] b = bl.get(i);
if (b[1] > 0) {
maxHeap.add(b[1]);
cur = maxHeap.peek();
} else {
maxHeap.remove(-b[1]);
cur = (maxHeap.peek() == null) ? 0 : maxHeap.peek();
}
if (cur != pre) {
res.add(new int[] { b[0], cur });
pre = cur;
}
}
return res;
}

Java for LeetCode 218 The Skyline Problem【HARD】的更多相关文章

  1. Java for LeetCode 126 Word Ladder II 【HARD】

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  2. LeetCode 218. The Skyline Problem 天际线问题(C++/Java)

    题目: A city's skyline is the outer contour of the silhouette formed by all the buildings in that city ...

  3. [LeetCode] 218. The Skyline Problem 天际线问题

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  4. [LeetCode#218] The Skyline Problem

    Problem: A city's skyline is the outer contour of the silhouette formed by all the buildings in that ...

  5. LeetCode:组合总数III【216】

    LeetCode:组合总数III[216] 题目描述 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. ...

  6. LeetCode:路径总和II【113】

    LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...

  7. LeetCode:括号的分数【856】

    LeetCode:括号的分数[856] 题目描述 给定一个平衡括号字符串 S,按下述规则计算该字符串的分数: () 得 1 分. AB 得 A + B 分,其中 A 和 B 是平衡括号字符串. (A) ...

  8. LeetCode:组合总数II【40】

    LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...

  9. LeetCode:整数转罗马数字【12】

    LeetCode:整数转罗马数字[12] 题目描述 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 10 ...

随机推荐

  1. linux静态库的生成与使用(转)

    linux静态库的生成与使用(转) 库是一种软件组件技术,库里面封装了数据和函数. 库的使用可以使程序模块化. Windows系统包括静态链接库(.lib文件)和动态链接库(.dll文件). Linu ...

  2. Windows下设置自动关机命令

    选择“开始→运行”:1.输入“at 22:00 Shutdown -s”,这样,到了22点电脑就会出现“系统关机”对话框,默认有30秒钟的倒计时并提示你保存工作.2.输入 “Shutdown.exe ...

  3. 更改vs自带的模板

    路径:C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplatesCache\CSharp\Code\20 ...

  4. SQLite初试...

    string dataPath = "../../UserData.dbx"; //System.IO.Directory.GetCurrentDirectory() + &quo ...

  5. PHP文件操作 读取与写入

    基本知识: PHP文件系统是基于Unix系统的 文件数据基本类型:二进制数据.文本数据 文件输入流:数据从源文件到内存的流动 文件输出流:数据从内存保存到文件的流动 文件操作函数: >>& ...

  6. UI第二节——UIButton详解

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launc ...

  7. NOSQL的学习

    NoSQL(NoSQL = Not Only SQL ),意即"不仅仅是SQL",指的是非关系型的数据库.NoSQL用于超大规模数据的存储.(例如谷歌或Facebook每天为他们的 ...

  8. HDU 1085 Holding Bin-Laden Captive!(DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1085 解题报告:有1,2,5三种面值的硬币,这三种硬币的数量分别是num_1,num_2,num_5, ...

  9. Python自动化之多进程

    多进程multiprocessing from multiprocessing import Process import os def info(title): print(title) print ...

  10. RPC(Remote Procedure Call Protocol)——远程过程调用协议 学习总结

        首先了解什么叫RPC,为什么要RPC,RPC是指远程过程调用,也就是说两台服务器A,B,一个应用部署在A服务器上,想要调用B服务器上应用提供的函数/方法,由于不在一个内存空间,不能直接调用,需 ...