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

天际线为当前线段的最高高度,所以用最大堆处理,当遍历到线段右端点时需要删除该线段的高度,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. PHP函数file_get_contents()使用 https 协议时报错:SSL operation failed

    场景: file_get_contents() 函数是用于将文件的内容读入到一个字符串中,是读取文件内容常用的函数之一. 但是有时在服务器上使用file_get_contents() 函数请求http ...

  2. BZOJ 4500: 矩阵 带权并查集

    这个思路挺巧妙的 ~ 定义一行/列的权值为操作后所整体增加的值. 那么,我们会有若干个 $a[x]+b[y]=c$ 的限制条件. 但是呢,我们发现符号是不能限制我们的(因为可加可减) 所以可以将限制条 ...

  3. 探索Windows 10的CFG机制

    转载https://www.anquanke.com/post/id/85493 0x00 前言 随着操作系统开发人员一直在增强漏洞利用的缓解措施,微软在Windows 10和Windows 8.1 ...

  4. day03 数据基础

    1.列举字符串,列表,元组,字典每个常用的五个方法 字符串: strip() , lstrip(),restrip() count(),index(),find() startswith,endswi ...

  5. 【牛客】路径计数机 (树形dp 前缀和)

    题目描述 有一棵n个点的树和两个整数p, q,求满足以下条件的四元组(a, b, c, d)的个数:  1.$1\leq a,b,c,d \leq n$  2.点a到点b的经过的边数为p.  3.点c ...

  6. UDF——定制窗口

    获取实例句柄的代码来自:https://blog.csdn.net/xie1xiao1jun/article/details/22180815 在Fluent当中我们可以使用scheme来为Fluen ...

  7. webpack4.0构建项目流程

    webpack4.0构建项目流程,具体的就不一一唠叨了,这里给出构建流程步骤: 流程大图: 下载高清大图

  8. jenkins使用--安装文档

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

  9. portaudio使用笔记《转》

    原文链接:https://blog.csdn.net/gg_simida/article/details/77185755 介绍 PortAudio是一个免费.跨平台.开源的音频I/O库.看到I/O可 ...

  10. Java NIO Buffer中各种状态属性的含义

    关于NIO Buffer中的3个重要状态属性的含义: postion, limit与capacity. public class NioTest { public static void main(S ...