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

相关推荐
lishi_19917 小时前
一键部署MoviePilotV2实现NAS全自动追剧
python·媒体·moviepilot
EllinY7 小时前
CF2217E Definitely Larger 题解
c++·笔记·算法·构造
测试开发-学习笔记8 小时前
代码详细解释
python
u0119608238 小时前
ray-k8s部署
python
PAK向日葵10 小时前
我用 C++ 写了一个轻量级 Python 虚拟机,刚刚开源
c++·python·开源
玖釉-10 小时前
下一个排列:从字典序到原地算法的完整推导
数据结构·c++·windows·算法
IronMurphy10 小时前
【算法五十】62. 不同路径
算法
影寂ldy11 小时前
C#一维数组
算法
枕星而眠11 小时前
数据结构八大排序详解(一):四大简单排序
c语言·数据结构·c++·后端