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

相关推荐
跟橙姐学代码1 小时前
手把手教你玩转 multiprocessing,让程序跑得飞起
前端·python·ipython
LCS-3121 小时前
Python爬虫实战: 爬虫常用到的技术及方案详解
开发语言·爬虫·python
穷儒公羊1 小时前
第二章 设计模式故事会之策略模式:魔王城里的勇者传说
python·程序人生·设计模式·面试·跳槽·策略模式·设计规范
心本无晴.1 小时前
面向过程与面向对象
python
花妖大人1 小时前
Python用法记录
python·sqlite
站大爷IP2 小时前
用PyQt快速搭建桌面应用:从零到实战的实用指南
python
站大爷IP2 小时前
PyCharm:Python开发者的智慧工作台全解析
python
zhanghongyi_cpp2 小时前
linux的conda配置与应用阶段的简单指令备注
linux·python·conda
MThinker2 小时前
14.examples\01-Micropython-Basics\demo_yield.py 加强版
python·学习·智能硬件·micropython·canmv·k230
LiRuiJie2 小时前
基于LangChain + Milvus 实现RAG
python·langchain·milvus·rag