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

相关推荐
databook22 分钟前
Manim实现脉冲闪烁特效
后端·python·动效
程序设计实验室1 小时前
2025年了,在 Django 之外,Python Web 框架还能怎么选?
python
倔强青铜三2 小时前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试
用户2519162427116 小时前
Python之语言特点
python
刘立军6 小时前
使用pyHugeGraph查询HugeGraph图数据
python·graphql
聚客AI8 小时前
🙋‍♀️Transformer训练与推理全流程:从输入处理到输出生成
人工智能·算法·llm
数据智能老司机9 小时前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
大怪v10 小时前
前端:人工智能?我也会啊!来个花活,😎😎😎“自动驾驶”整起!
前端·javascript·算法
数据智能老司机10 小时前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构
c8i12 小时前
django中的FBV 和 CBV
python·django