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

相关推荐
Storynone9 分钟前
【Day20】LeetCode:39. 组合总和,40. 组合总和II,131. 分割回文串
python·算法·leetcode
小鸡吃米…27 分钟前
Python—— 环境搭建
python
io_T_T33 分钟前
python 文件管理库 Path 解析(详细&基础)
python
明明如月学长40 分钟前
AI 更新太快学不过来?我用OpenClaw打造专属AI学习工作流
算法
黎阳之光1 小时前
【黎阳之光:以无线专网与视频孪生,赋能智慧广电与数字中国】
算法·安全·智慧城市·数字孪生
刀法如飞2 小时前
Agentic AI时代,程序员必备的算法思想指南
人工智能·算法·agent
刀法如飞2 小时前
Agentic AI时代程序员必备算法思想详解(附实战案例)
算法·ai编程·编程开发·agentic
渔阳节度使2 小时前
SpringAI实时监控+观测性
后端·python·flask
铁手飞鹰2 小时前
Visual Studio创建Cmake工程导出DLL,通过Python调用DLL
android·python·visual studio
飞Link3 小时前
告别盲目找Bug:深度解析 TSTD 异常检测中的预测模型(Python 实战版)
开发语言·python·算法·bug