天际线问题,参考自: 百草园

天际线为当前线段的最高高度,所以用最大堆处理,当遍历到线段右端点时需要删除该线段的高度,priority_queue不提供删除的操作,要用unordered_map来标记要删除的元素。从heap中pop的时候先看有没有被标记过,如果标记过,就一直pop直到空或都找到没被标记过的值(之前的值被标记过也要删除干净,因为到当前x坐标时,标记过的高度已失效)。为排除冗余答案,需要提前对线段排序,横坐标相等时,都是左端点,按y从大到小(只记录高的端点),都是右端点,按y从小到大(只记录高的端点),一左一右,左在前右在后,不重复记录。

 class Solution {
private:
enum NODE_TYPE {LEFT, RIGHT};
struct node{
int x,y;
NODE_TYPE type;
node(int _x, int _y, NODE_TYPE _type): x(_x),y(_y),type(_type){}
}; public:
vector<vector<int>> getSkyline(vector<vector<int>>& buildings) {
vector<node> height;
for(int i=;i<buildings.size();i++){
height.push_back(node(buildings[i][],buildings[i][],LEFT));
height.push_back(node(buildings[i][],buildings[i][],RIGHT));
}
sort(height.begin(),height.end(),[](const node &a, const node& b) {
if(a.x!=b.x)return a.x<b.x;
else if(a.type==b.type && a.type == LEFT) return a.y>b.y;
else if(a.type==b.type && a.type == RIGHT) return a.y<b.y;
else return a.type == LEFT;
});
priority_queue<int> heap;
heap.push();
unordered_map<int,int> mp; // remove the element in heap
int cur=,pre=;
vector<vector<int>> res;
for(auto & h : height){
if(h.type == LEFT){
heap.push(h.y);
} else {
mp[h.y]++;
while(!heap.empty()&&mp[heap.top()]>) {
mp[heap.top()]--;
heap.pop();
}
}
cur = heap.top();
if(cur!=pre){
res.push_back({h.x,cur});
pre = cur;
}
}
return res;
}
};

218. The Skyline Problem (LeetCode)的更多相关文章

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

  2. [LeetCode#218] The Skyline Problem

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

  3. Java for LeetCode 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 ...

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

  5. 218. The Skyline Problem

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

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

  7. The Skyline Problem leetcode 详解

    class Solution { public: vector<pair<int, int>> getSkyline(vector<vector<int>&g ...

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

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

  9. [LeetCode] The Skyline Problem

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

随机推荐

  1. Spring搬迁

      Spring简介 加载bean流程 Bean的生命周期 双亲委派 自定义类加载器 Spring事务 异步Async Spring设计模式 Spring单例 SpringMVC流程   备注:app ...

  2. python操作excel获取内容

    背景:从excel表中获取请求url.请求数据.请求类型.预期结果 因此,需要学会如何使用python从excel获取这些信息 #coding=utf-8 import xlrd #创建对象时,获取对 ...

  3. strings命令(转)

    以前我用strings命令的频率不高, 现在越用越顺手, 而且已经是离不开strings命令了.虽然以前说过strings命令, 但今天还是要说. 主要是两大用途, 下面来说明一下: 一. 确认代码编 ...

  4. Hibernate——离线查询

    1.Criteria查询方式: (1)一般方式: 缺点:每一次查询dao层都需要书写对应的方法,离线查询可以解决这个问题. (2)离线方式: 2.离线查询 用DetachedCriteria来构造查询 ...

  5. 网络命令——write、wall、ping、ifconfig、mail

    1.write命令: 前提:用户必须在线: (1)向zhb用户发送信息: (2)用户收到信息: CTRL+D结束会话. 2.wall(发送广播信息,即给在线的所有用户发送信息) 管理员发送消息(自己可 ...

  6. ES5新增的数组方法

    ES5新增:(IE9级以上支持)1.forEach():遍历数组,无返回值,不改变原数组.2.map():遍历数组,返回一个新数组,不改变原数组.3.filter():过滤掉数组中不满足条件的值,返回 ...

  7. stack的简单用法总结

    stack中常见方法 top():返回一个栈顶元素的引用,类型为 T&.如果栈为空,返回值未定义. push(const T& obj):可以将对象副本压入栈顶.这是通过调用底层容器的 ...

  8. cocos Uniforms值的赋值

    cocos Uniforms值的赋值 举个void Renderer::drawBatchedTriangles()的渲染: //Start drawing verties in batch for( ...

  9. 19 条效率至少提高 3 倍的 MySQL 技巧

    阅读本文大概需要 4 分钟. 来源:https://zhuanlan.zhihu.com/p/49888088 本文我们来谈谈项目中常用的 MySQL 优化方法,共 19 条,具体如下: 1.EXPL ...

  10. jenkins使用--安装文档

    添加Jenkins的源(repository): #sudo wget -O /etc/yum.repos.d/jenkins.repo http://jenkins-ci.org/redhat/je ...