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. Mysql锁和死锁分析

    在MySQL中,行级锁并不是直接锁记录,而是锁索引.索引分为主键索引和非主键索引两种,如果一条sql语句操作了主键索引,MySQL就会锁定这条主键索引;如果一条语句操作了非主键索引,MySQL会先锁定 ...

  2. 人工智能VS投资者:股票市场上谁的胜算更高?

    人工智能研究历史渊源,当人工智能与资本投资,尤其是股票投资相结合或许听起来有些异想天开,但正如科幻作家William Gibson所言:"未来已经到来,只是分布不均." 在股票市场 ...

  3. 【01】React 环境搭建

    react来自于Facebook公司的开源项目 react 组件化模块化  开发模式 react通过对DOM的模拟(虚拟dom),最大限度地减少与DOM的交互  (数据绑定) react 基于jsx的 ...

  4. 无线个人区域网WPAN

    无线个人区域网WPAN (Wireless Personal Area Network) 1.1.概述 在个人工作地方把属于个人使用的电子设备用无线技术连接起来自组网络,不需要使用接入点 AP. 整个 ...

  5. 达拉草201771010105《面向对象程序设计(java)》第十一周学习总结

    达拉草201771010105<面向对象程序设计(java)>第十一周学习总结 实验十一   集合 实验时间 2018-11-8 第一部分:理论知识 1.集合(Collection或称为容 ...

  6. Mariadb 修改root密码及跳过授权方式启动数据库

    默认情况下,yum方式新安装的 mariadb 的密码为空,在shell终端直接输入 mysql 就能登陆数据库. 如果是刚安装第一次使用,请使用 mysql_secure_installation ...

  7. 在idea下遇到的问题汇总(间接性更新)

    在idea下遇到的问题汇总(间接性更新) tomcat下的jsp代码问题: 在idea的环境下,遇到jsp代码.符号失效,首先需要考虑到jar包没有引入,情况如图: 这种情况是因为jar包没有导入进去 ...

  8. JMeter-接口测试之数据驱动

    前言 之前我们的用例数据都是配置在Http 请求中,每次需要增加,修改用例都需要打开 jmeter 重新编辑,当用例越来越多的时候,用例维护起来就越来越麻烦,有没有好的方法来解决这种情况呢?我们可以将 ...

  9. python2.7.6安装easy_install (windows 64 环境)

    1.复制以下代码保存到easy_install.py文件中(文件名可随意命名)并将该文件放到python的安装路径中(如:D:\Python27) #!/usr/bin/env python &quo ...

  10. 使用nestjs集成grpc具体操作

    两个程序中, 提供grpc服务的称为服务端, 调用grpc服务的为客户端, 以下是grpc服务端和客户端的代码编写     1. 创建两个nestjs项目demo1(端口: 3000)和demo2(端 ...