The Skyline Problem leetcode 详解

class Solution {
public:
    vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
        vector<pair<int, int> > h, res;
        multiset<int> m;
        int pre = 0, cur = 0;
        for (auto &a : buildings) {
            h.push_back({a[0], -a[2]});
            h.push_back({a[1], a[2]});
        }
        sort(h.begin(), h.end());
        m.insert(0);
        for (auto &a : h) {
            if (a.second < 0) 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;
    }
};
时间: 2024-10-25 15:30:33

The Skyline Problem leetcode 详解的相关文章

Leetcode 详解(ReverseWords)

Leetcode里面关于字符串的一些问题,描述如下: Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". Update (2015-02-12):For C programmers: Try to solve it inO(1) space. click to show cla

Leetcode 详解(valid plindrome)

Question: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome.Example Questions C

Leetcode 详解(Valid Number)

Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => true 解决方案: public class Solution { public boolean isNumber(String s) { in

Leetcode 详解(股票交易日)(动态规划DP)

问题描述: 在股市的交易日中,假设最多可进行两次买卖(即买和卖的次数均小于等于2),规则是必须一笔成交后进行另一笔(即买-卖-买-卖的顺序进行).给出一天中的股票变化序列,请写一个程序计算一天可以获得的最大收益.请采用实践复杂度低的方法实现. 给定价格序列prices及它的长度n,请返回最大收益.保证长度小于等于500. class Solution { public: int maxProfit(vector<int>& prices) { //It's wrong if you c

Leetcode 详解(Substing without repeats character)

Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1.

【leetcode】Edit Distance 详解

下图为TI C6xx DSP Nyquist总线拓扑图,总线连接了master与slave,提供了高速的数据传输.有很多种速率不同的总线,如图中的红色方框,最高速总线为CPU/2 TeraNet SCR(即VBUSM SCR),带宽为256bit,其他低速总线为CPU/3,CPU/6,带宽参考图中所示.总线之间用Bridge(桥)连接,作用包括转换总线的速率,使之与所流向总线的速率相同等. 在具体应用中,各种速率的总线完全可以满足复杂的数据传输,而数据传输的瓶颈往往在于连接总线之间的Bridge

LeetCode ---Anagrams() 详解

Notice: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. For example: Input: ["tea","and","ate","eat","den"] Output:   ["tea",&qu

LeetCode Go 并发题详解:交替打印字符串

原文地址:https://mp.weixin.qq.com/s/K032xlARjiyS8ecJrqZXaA 本题 LeetCode 链接: https://leetcode.com/problems/fizz-buzz-multithreaded/ 本题题目 给定一个数列从 1 ~ n,依序输出,但是: 如果 n 可以被 3 整除,输出 "fizz" 如果 n 可以被 5 整除,输出 "buzz" 如果 n 同时可以被 3 与 5 整除,输出 "fizz

leetcode之反转链表图文详解

206-反转链表 题目: 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 思路: 迭代法: 新建一个链表的头部,循环遍历旧链表的结点,将其加到新链表的后面 递归法 代码:(迭代法) /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; *