python学习笔记1-SortedList的使用

题目链接

  • SortedList增删改查的复杂度均为 O ( l o g n ) O(logn) O(logn)
  • 四种操作分别为:
    add, remove/discard/pop(弹出某个索引的元素,默认为最大值), count/index
python 复制代码
from sortedcontainers import SortedList

class StockPrice:

    def __init__(self):
        self.price = SortedList()
        self.timePriceMap = {}
        self.maxTimestamp = 0

    def update(self, timestamp: int, price: int) -> None:
        if timestamp in self.timePriceMap:
            self.price.discard(self.timePriceMap[timestamp]) # 删除原本的时间
        self.price.add(price)
        self.timePriceMap[timestamp] = price
        self.maxTimestamp = max(self.maxTimestamp, timestamp)

    def current(self) -> int:
        return self.timePriceMap[self.maxTimestamp]

    def maximum(self) -> int:
        return self.price[-1]

    def minimum(self) -> int:
        return self.price[0]



# Your StockPrice object will be instantiated and called as such:
# obj = StockPrice()
# obj.update(timestamp,price)
# param_2 = obj.current()
# param_3 = obj.maximum()
# param_4 = obj.minimum()

参考

https://www.cnblogs.com/baiyutang7/p/16541848.html

https://zhuanlan.zhihu.com/p/545601213

相关推荐
pp起床14 分钟前
Part02:基本概念以及基本要素
大数据·人工智能·算法
lzh2004091918 分钟前
红黑树详解
算法
迈巴赫车主31 分钟前
蓝桥杯20560逃离高塔
java·开发语言·数据结构·算法·职场和发展·蓝桥杯
泯仲39 分钟前
Ragent项目7种设计模式深度解析:从源码看设计模式落地实践
java·算法·设计模式·agent
dulu~dulu43 分钟前
算法---寻找和为K的子数组
笔记·python·算法·leetcode
moonsea02031 小时前
【无标题】
算法
编程之升级打怪1 小时前
用Python语言实现简单的Redis缓冲数据库驱动库
redis·python
佑白雪乐1 小时前
<ACM进度212题>[2026-3-1,2026-3-26]
算法·leetcode
穿条秋裤到处跑1 小时前
每日一道leetcode(2026.03.26):等和矩阵分割 II
算法·leetcode·矩阵
平凡灵感码头1 小时前
C语言 printf 数据打印格式速查表
c语言·开发语言·算法