218. The Skyline Problem (LeetCode)
天际线问题,参考自: 百草园
天际线为当前线段的最高高度,所以用最大堆处理,当遍历到线段右端点时需要删除该线段的高度,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)的更多相关文章
- [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 ...
- [LeetCode#218] The Skyline Problem
Problem: A city's skyline is the outer contour of the silhouette formed by all the buildings in that ...
- 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 ...
- 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 ...
- 218. The Skyline Problem
题目: A city's skyline is the outer contour of the silhouette formed by all the buildings in that city ...
- 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 ...
- The Skyline Problem leetcode 详解
class Solution { public: vector<pair<int, int>> getSkyline(vector<vector<int>&g ...
- [LeetCode] The Skyline Problem 天际线问题
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- [LeetCode] The Skyline Problem
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
随机推荐
- LeetCode 934. Shortest Bridge
原题链接在这里:https://leetcode.com/problems/shortest-bridge/ 题目: In a given 2D binary array A, there are t ...
- Xamarin.Forms之XAML
官网参考 XAML基础知识 XAML(eXtensible Application Markup Language)可扩展应用程序标记语言,允许开发者在Xamarin.Forms应用中采用标记而不是代 ...
- Xamarin.Forms快速入门-深入探讨
官网链接 项目介绍 以Notes项目为例,The Notes application consists of one solution containing four projects, as sho ...
- cogs 998. [東方S2] 帕秋莉·诺蕾姬
二次联通门 : cogs 998. [東方S2] 帕秋莉·诺蕾姬 交上去后发现自己没上榜 就想着加点黑科技 把循环展开一下 结果WA了.. 万恶的姆Q /* cogs 998. [東方S2] 帕秋莉· ...
- 假设检验总结以及如何用python进行假设检验(scipy)
几种常见的假设检验总结如下: 假设检验名称 Z检验 t检验 χ2检验 F检验 原假设 H0: μ≥μ0 H0: μ≤μ0 H0: μ=μ0 (比较样本和总体均值) ...
- 记一次CPU使用100%问题排查
需求 前端同事说测试环境的服务接口查起来很慢,很不稳定,不是个别接口,而是大量接口. 情况分析 由于是在测试环境联调,没有多少用户量.第一步:先去服务器看看资源的使用情况.使用top命令,查看cpu的 ...
- 二叉树 排序二叉树-可以通过中序遍历得到排序的数据 二叉排序树时间复杂度O(logn),
二叉树是一种非常重要的数据结构,它同时具有数组和链表各自的特点:它可以像数组一样快速查找,也可以像链表一样快速添加.但是他也有自己的缺点:删除操作复杂. 虽然二叉排序树的最坏效率是O(n),但它支持动 ...
- 【Python】[技术博客] 一些使用Python编写获取手机App日志的操作
一些使用Python编写获取手机App日志的操作 如何获取手机当前打开的App的包名 如何获取当前App进程的PID 如何查看当前App的日志 如何将日志保存到文件 如何关闭进程 如何不显示命令行窗口 ...
- C++的static_cast、dynamic_cast和const_cast用法
static_cast.dynamic_cast和const_cast static_cast: 用法: static_cast < type-id > (expression) ...
- Qt编写安防视频监控系统15-远程回放
一.前言 远程回放有两种处理方式,一种是采用NVR厂家提供的SDK开发包来登录到NVR上,然后根据SDK的函数接口指定的视频文件,当然也有接口查询视频文件列表等:一种是采用视频监控行业的国标GB281 ...