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

相关推荐
_zwy2 分钟前
【Python 入门基础】—— 人工智能“超级引擎”,AI界的“瑞士军刀”,
开发语言·人工智能·python·深度学习
微笑的Java15 分钟前
Python - 代码片段分享 - Excel 数据实时写入方法
开发语言·python·excel
Ritsu栗子17 分钟前
代码随想录算法训练day63---图论系列7《prim算法&kruskal算法》
c++·算法·图论
一只码代码的章鱼25 分钟前
数据结构与算法-图论-最短路-单源最短路的建图方式
算法·图论
黄金龙PLUS34 分钟前
十分简单的流密码算法RC4
算法·网络安全·密码学·哈希算法·同态加密
噗噗bug37 分钟前
数据结构 1-2 线性表的链式存储-链表
数据结构·链表
我想吃余38 分钟前
【初探数据结构】时间复杂度和空间复杂度
数据结构·算法
a_j5839 分钟前
算法与数据结构(环形链表II)
数据结构·算法·链表
冷琴19961 小时前
基于python+django的宠物商店-宠物管理系统源码+运行步骤
python·django·宠物
Reese_Cool1 小时前
【爬虫】request库
爬虫·python