首页 > 其他分享 >leetcode(34)优先队列系列题目

leetcode(34)优先队列系列题目

时间:2022-11-07 11:57:27浏览次数:60  
标签:pre cur 队列 List leetcode 34 mx change 边界

218. 天际线问题

用SortedList存边界,每次删除或加入边界判断最高点是否变化

class Solution:
    def getSkyline(self, buildings: List[List[int]]) -> List[List[int]]:
        from sortedcontainers import SortedList
        change = []
        for l, r, h in buildings:
            change.append((l, -h))
            change.append((r, h))
        change.sort()
        pre = 0
        res = []
        s = SortedList([0])
        for i, h in change:
            if h < 0:
                s.add(h)  # 存入左边界
            else:
                s.remove(-h)  # 删除右边界
            cur_mx = -s[0]
            if cur_mx != pre:  # 存入或删除边界后可能发生改变
                pre = cur_mx
                res.append((i, cur_mx))
        return res

标签:pre,cur,队列,List,leetcode,34,mx,change,边界
From: https://www.cnblogs.com/ttyangY77/p/16865444.html

相关文章

  • 用Rust刷leetcode第八题
    ProblemImplement ​​atoi​​​ which convertsastringtoaninteger.Thefunctionfirstdiscardsasmanywhitespacecharactersasnecessaryuntilthefirst......
  • [leetcode每日一题]11.7
    816. 模糊坐标我们有一些二维坐标,如 ​​"(1,3)"​​ 或 ​​"(2,0.5)"​​,然后我们移除所有逗号,小数点和空格,得到一个字符串​​S​​。返回所有可能的原始字符串到......
  • 025 通过链表学Rust之使用栈实现双端队列
    介绍视频地址:https://www.bilibili.com/video/av78062009/相关源码:https://github.com/anonymousGiga/Rust-link-list详细内容本节我们使用栈来实现双端队列。实现栈栈的实......
  • [LeetCode] 1678. Goal Parser Interpretation
    Youowna GoalParser thatcaninterpretastring command.The command consistsofanalphabetof "G", "()" and/or "(al)" insomeorder.TheGoalPar......
  • leetcode 54. 螺旋矩阵 js高效实现
    给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。示例1:  输入:matrix=[[1,2,3],[4,5,6],[7,8,9]]输出:[1,2,3,6,9,8,7,4,5]......
  • LeetCode刷题记录.Day7
    有效的字母异位词题目链接242.有效的字母异位词-力扣(LeetCode)classSolution{public:boolisAnagram(strings,stringt){intrecord[26]={0};......
  • 14-组件篇之消息队列(3)_ev
                                                   ......
  • CF1349F1 Slime and Sequences (Easy Version)
    linkSolution以前看到过,但是一直没有做......
  • leetcode318
    6231.雇佣K位工人的总代价题意:一个数组表示雇佣工人花费,k表示需要雇佣的人数,从数组前后各选candidates个人,从中选花费最小下标最小的工人,然后数组更新,重复上述操作,直至......
  • #yyds干货盘点# LeetCode 腾讯精选练习 50 题:二叉树中的最大路径和
    题目:路径被定义为一条从树中任意节点出发,沿父节点-子节点连接,达到任意节点的序列。同一个节点在一条路径序列中至多出现一次。该路径至少包含一个节点,且不一定经过根节......