Leetcode 2034. Stock Price Fluctuation

这篇博客讨论了LeetCode上的第2034题,即股票价格波动问题。作者探讨了三种解法,包括被时间限制的暴力解法、使用堆(heap)优化的解法,以及另外一种直观但难以理解的解法。关键在于如何有效地找到最小值和最大值,作者在使用堆的解法中提到,需要更新并维护两个堆,并处理时间戳重复出现的情况。
摘要由CSDN通过智能技术生成

题目

在这里插入图片描述

解法:暴力解法(tle)

这道题的关键当然是在于找min和max,很自然的想到了用heap。但是没太想明白怎么同时保持这两个heap

class StockPrice:

    def __init__(self):
        self.time2price = {
   }
        self.curr_price = None
        self.curr_time = None
        self.min_price = 1000000000
        self.max_price = 0

    def update(self, timestamp: int, price: int) -> None:
        if self.curr_time == None:
            self.curr_time = timestamp
            self.curr_price = price
        else:
            if timestamp >= self.curr_time:
                self.curr_time = timestamp
                self.curr_price = price
        # if self.min_price == None:
        #     self.min_price = price
        #     self.max_price = price
        #     self.time2price[timestamp] = price
        #     return
        if timestamp not in self.time2price:
            self
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值