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

相关推荐
Iruoyaoxh12 分钟前
数据结构排序
数据结构
databook1 小时前
用递归消除法优化“特征子集”
python·机器学习·scikit-learn
CAE二次开发工程师1 小时前
【C语言入门到精通】-基础篇
c语言·开发语言·python
来一碗刘肉面1 小时前
图的基本操作
数据结构
深圳市快瞳科技有限公司1 小时前
个体识别、行为解读、健康管理:多模态宠物AI大模型的场景化落地
人工智能·算法·计算机视觉·大模型·多模态·宠物·宠物ai识别
liulilittle2 小时前
归一化:激活函数
人工智能·算法·机器学习·llm
森G2 小时前
comfyui安装ComfyUI-Manager失败问题-解决办法
python
过期的秋刀鱼!2 小时前
倾斜数据集的错误指标-混淆矩阵
人工智能·python·深度学习·机器学习·概率论·模型评估
能年玲奈喝榴莲牛奶3 小时前
使用AI编写-资产和漏洞管理系统
人工智能·python·网络安全·安全服务
Mem0rin3 小时前
前缀和:一维与二维
数据结构·算法