Day58力扣打卡

打卡记录

下一个更大元素 IV(单调栈 x2)

链接

python 复制代码
class Solution:
    def secondGreaterElement(self, nums: List[int]) -> List[int]:
        ans = [-1] * len(nums)
        s = []
        t = []
        for i, x in enumerate(nums):
            while t and nums[t[-1]] < x:
                ans[t.pop()] = x  # t 栈顶的下下个更大元素是 x
            j = len(s) - 1
            while j >= 0 and nums[s[j]] < x:
                j -= 1  # s 栈顶的下一个更大元素是 x
            t += s[j + 1:]  # 把从 s 弹出的这一整段元素加到 t
            del s[j + 1:]  # 弹出一整段元素
            s.append(i)  # 当前元素(的下标)加到 s 栈顶
        return ans
相关推荐
荷蒲1 分钟前
【小白量化Qbuddy】用AI设计miniQMT指标公式计算量化平台
人工智能·python·机器人
阿童木写作5 分钟前
跨境电商图片翻译工具,批量翻译视频字幕一键抠图
人工智能·python·音视频
多弗朗皮卡丘43 分钟前
算法详解4:买卖股票的最佳时机系列(上)
算法
何以解忧,唯有..1 小时前
Pydantic 介绍与使用:Python 数据校验的现代方案
数据库·python·microsoft
又幸福了哥2 小时前
Python入门到高级(知识点七)
python
又幸福了哥3 小时前
Python入门到高级(知识点六)
python
维克兜率天3 小时前
【维克】动量指标家族:RSI、ROC、CCI、Momentum全面解析
python·算法
AI情绪识别开源4 小时前
检信 AI 智能推广平台(代号:JX-Promote)
人工智能·算法·erlang
XZ-0700014 小时前
week2-1-可视化
开发语言·python
Java后端的Ai之路4 小时前
14、Python - 责任链模式
服务器·开发语言·人工智能·python·责任链模式