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

相关推荐
数据知道2 分钟前
一文掌握向量数据库Chroma的详细使用
数据库·python·向量数据库
火羽白麟13 分钟前
大坝安全的“大脑”——模型与算法
算法·模型·大坝安全
x70x8016 分钟前
C++中auto的使用
开发语言·数据结构·c++·算法·深度优先
xu_yule21 分钟前
算法基础-单源最短路
c++·算法·单源最短路·bellman-ford算法·spfa算法
计算机毕设指导623 分钟前
基于微信小程序+django连锁火锅智慧餐饮管理系统【源码文末联系】
java·后端·python·mysql·微信小程序·小程序·django
colourmind24 分钟前
记录一次vscode debug conda python 使用报错问题排查
vscode·python·conda
Evand J26 分钟前
【MATLAB免费例程】多无人机,集群多角度打击目标,时间与角度约束下的协同攻击算法,附下载链接
算法·matlab·无人机
智航GIS27 分钟前
2.1 变量与数据类型
开发语言·python
旧梦吟28 分钟前
脚本工具 批量md转html
前端·python·html5
YGGP29 分钟前
【Golang】LeetCode 118. 杨辉三角
算法·leetcode