2020-01-10 17:51:05

问题描述

问题求解

本题是经典的sweep line问题。

对于sweep line问题我们需要考虑的只有两点:

1. 延水平方向 / 时间方向 :时间队列 event queue,一般来说是一个优先队列;

2. 延垂直方向sweep line status,即当前的扫描线的状态,一般会将交点按照顺序排序;

对于本题来说,sweep line status可以使用一个multi set来进行维护,当然由于在Java中没有multi set,因此需要使用TreeMap来模拟。

event queue的当然是使用优先队列,问题是如何进行排序,这个才是本题的核心难点。

这里给出结论:

大方向是按照x轴排序,如果x轴相等那么按照height排序;

如果x轴相等,优先判断是否是左端点,如果是左端点,那么优先入队;

如果同时是右端点,那么需要反序入队,就是height小的反而需要排在前面。

    public List<List<Integer>> getSkyline(int[][] buildings) {
List<List<Integer>> res = new ArrayList<>();
// 对于同x轴,优先将左端点入队列
// 如果同是右端点,则要反序,小的先入队列
// 其余按照正常的height顺序排列即可
PriorityQueue<int[]> pq = new PriorityQueue<>(new Comparator<int[]>(){
public int compare(int[] o1, int[] o2) {
if (o1[0] == o2[0] && o1[2] != o2[2]) return o1[2] - o2[2];
if (o1[0] == o2[0] && o1[2] == 1 && o1[2] == o2[2]) return o1[1] - o2[1];
return o1[0] == o2[0] ? o2[1] - o1[1] : o1[0] - o2[0];
}
});
TreeMap<Integer, Integer> map = new TreeMap<>();
for (int[] b : buildings) {
int s = b[0];
int e = b[1];
int h = b[2];
pq.add(new int[]{s, h, 0});
pq.add(new int[]{e, h, 1});
}
while(!pq.isEmpty()) {
int[] event = pq.poll();
int x = event[0];
int h = event[1];
int status = event[2];
int curr_max = map.size() == 0 ? 0 : map.lastKey();
if (status == 0) {
if (h > curr_max) res.add(Arrays.asList(x, h));
map.put(h, map.getOrDefault(h, 0) + 1);
}
else {
map.put(h, map.get(h) - 1);
if (map.get(h) == 0) map.remove(h);
curr_max = map.size() == 0 ? 0 : map.lastKey();
if (h > curr_max) res.add(Arrays.asList(x, curr_max));
} }
return res;
}

  

sweep line-The Skyline Problem的更多相关文章

  1. 218. The Skyline Problem *HARD* -- 矩形重叠

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

  2. Sweep Line

    391. Number of Airplanes in the Sky https://www.lintcode.com/problem/number-of-airplanes-in-the-sky/ ...

  3. [LeetCode] 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] The Skyline Problem

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

  5. The Skyline Problem

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

  6. [LeetCode#218] The Skyline Problem

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

  7. [LeetCode] 281. The Skyline Problem 天际线问题

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

  8. [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 ...

  9. 218. The Skyline Problem

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

随机推荐

  1. 生死状:苹果VS他的供应商

    据知情人士透露,苹果已经组建了代号为Titan的汽车团队,并招募了数百名员工,准备进入汽车领域,iCar大有呼之欲出之势.事实上,苹果CEO蒂姆-库克早在去年就参观了宝马位于莱比锡的核心工厂,学习如何 ...

  2. Ubuntu18.04安装OpenStack

    Ubuntu18.04 安装Queens版本OpenStack 安装环境 系统 系统使用的是Ubuntu18,最少4核8G内存,20G硬盘空间. 工具 devstack DevStack是一系列可扩展 ...

  3. Windows 下 LaTeX 手动安装宏包(package)以及生成帮助文档的整套流程

    本文简单介绍如何手动安装一个 LaTeX 宏包. 一般来说,下载的 TeX 发行版已经自带了很多宏包,可以满足绝大部分需求,但是偶尔我 们也可能碰到需要使用的宏包碰巧没有安装的情况,这时我们就需要自己 ...

  4. Android中Intent的各种常见作用。

    Android开发之Intent.Action  1 Intent.ACTION_MAIN String: android.intent.action.MAIN 标识Activity为一个程序的开始. ...

  5. 一天速成Python教程

    一.Python基础 Python是对象有类型,变量无类型的动态类型语言,追求简单优雅易读.可以在终端中逐行运行,也可以编写成大型的面向对象的工程.在开始写之前,注意Python 2.X中,开头要写上 ...

  6. 选题在线提交系统(html+JS+php)

    前言:         作为学习委员还是有挺多的事情要忙的,比如经常统计同学们的课设题目选择结果.如果老师的要求少一点,我还可以轻松一点.但是当老师对选题有种种限制的时候,自己就估计不会那么好办了.这 ...

  7. 组件化思路:以selection为例子,使用prop-types实现组件化控制,重用

    需求 书接上文,UI 积累之select section 这里又来两个需求了. 当我点击选择了option后,我应该看到的是我选择的option的内容 多例重用,即同样是个selection,我只是需 ...

  8. Go性能分析大杀器PPROF

    这是什么 想要进行性能优化,Go本身自带的工具链就包含了性能分析工具,而且也非常棒,pprof就是Go性能分析的利器,它是Go语言自带的包,有如下两种: runtime/pprof:采集程序(非 Se ...

  9. python之函数介绍

    # 函数 # 什么是函数: 能完成特定功能的工具,在Python中表示能完成特定功能的代码块.(函数定义) # 为什么要用函数 :①函数可以重复调用出来,效率高,而且维护成本低 ②使程序结构看起来清晰 ...

  10. 添加谷歌拓展程序 vue.js devtools过程中的问题

    在用vue做项目过程中,需要用到vue.js devtools,在从github上面clone下来代码,然后再npm install ,过程报错,然后更新npm包也是会有问题,以下是install的问 ...