The Skyline Problem leetcode 详解

思路:
假设柱子是
{ [0,1,4], [0,2,3], [3,5,3] }
每次扫描一条竖线后,对m进行竖线的压入或者弹出m中对应的左半边的操作,之后用cur表示处理完后当前所在的水平线,prev表示上一次天线点所在的水平线
prev初始化是0,这样碰到第一个柱子由于它高度大于0,一定会产生一个结果,prev = cur = 4
class Solution {
public:
vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
vector<pair<int, int> > h, res;
multiset<int> m;
int pre = , cur = ;
for (auto &a : buildings) {
h.push_back({a[], -a[]});
h.push_back({a[], a[]});
}
sort(h.begin(), h.end());
m.insert();
for (auto &a : h) {
if (a.second < ) m.insert(-a.second);
else m.erase(m.find(a.second));
cur = *prev(m.end());
if (cur != pre) {
res.push_back({a.first, cur});
pre = cur;
}
}
return res;
}
};
The Skyline Problem leetcode 详解的更多相关文章
- 由Leetcode详解算法 之 动态规划(DP)
因为最近一段时间接触了一些Leetcode上的题目,发现许多题目的解题思路相似,从中其实可以了解某类算法的一些应用场景. 这个随笔系列就是我尝试的分析总结,希望也能给大家一些启发. 动态规划的基本概念 ...
- Leetcode 详解(ReverseWords)
Leetcode里面关于字符串的一些问题,描述如下: Given an input string, reverse the string word by word. For example,Given ...
- Leetcode 详解(valid plindrome)
Question: Given a string, determine if it is a palindrome, considering only alphanumeric characters ...
- Leetcode 详解(股票交易日)(动态规划DP)
问题描述: 在股市的交易日中,假设最多可进行两次买卖(即买和卖的次数均小于等于2),规则是必须一笔成交后进行另一笔(即买-卖-买-卖的顺序进行).给出一天中的股票变化序列,请写一个程序计算一天可以获得 ...
- Leetcode详解Maximum Sum Subarray
Question: Find the contiguous subarray within an array (containing at least one number) that has the ...
- Leetcode 详解(Substing without repeats character)
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- Leetcode 详解(Valid Number)
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- Leetcode 详解(Implement strstr)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 218. The Skyline Problem (LeetCode)
天际线问题,参考自: 百草园 天际线为当前线段的最高高度,所以用最大堆处理,当遍历到线段右端点时需要删除该线段的高度,priority_queue不提供删除的操作,要用unordered_map来标记 ...
随机推荐
- sqlserver2005级联删除
在你建表,建主外键的时候,在下面有几个选项,有一个是级联删除和一个级联更新,勾选上就可以了
- 从一般分布式设计看HDFS设计思想与架构
要想深入学习HDFS就要先了解其设计思想和架构,这样才能继续深入使用HDFS或者深入研究源代码.懂得了"所以然"才能在实际使用中灵活运用.快速解决遇到的问题.下面这篇博文我们就先 ...
- 【实用技巧】去除BootStrap所有圆角效果
满屏的圆角有没有审"美"疲劳啊?教你一键去除所有圆角效果: * { -webkit-border-radius: 0 !important; -moz-border-radius: ...
- Android KeyLogger Demo
@author: dlive 代码见github: https://github.com/Dliv3/Android-KeyLogger-Demo 前言 之前开发过一个Android的木马,其中Key ...
- 微型orm框架--dapper的简单使用
1.安装 首先使用nuget安装dapper,因为这里的示例是使用mysql,所以还要安装mysql的驱动.如下图: 2 数据库表 脚本 ; -- -------------------------- ...
- MVC源码解析 - HttpRuntime解析
先看一张图, 从这张图里, 能看到请求是如何从CLR进入HttpRuntime的. 一.AppManagerAppDomainFactory 看到这张图是从 AppManagerAppDomainFa ...
- mysql数据一致性检查及修复
percona-toolkit-2.2.20-1.noarchmysql 5.6.29-logmaster:192.168.166.129slave:192.168.166.131 一.创建数据库校验 ...
- jquery新版本不支持toggle()的解决方法
toggle() 方法用于绑定两个或多个事件处理器函数,以响应被选元素的轮流的 click 事件. 在1.9以后官方废除了这个方法: 解决如下 在需要调用的js文件下引用 $.fn.toggle = ...
- Selenium也是一个用于Web应用程序测试的工具
Selenium也是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE.Mozilla Firefox.Mozilla Suite ...
- 通过file文件选择图片预览功能
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...