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
相关推荐
霸道流氓气质27 分钟前
RAG 可观测性与生产监控实战:四层遥测模型、五大仪表盘与 OpenTelemetry 全链路追踪的 Java 生产级实现
java·人工智能·python
碧海银沙音频科技研究院2 小时前
基于炬芯ATS3089X自研AEC声学回声消除算法
算法
ch6232 小时前
《AI 辅助编程实战:从零跑通一个 Excel 报表生成脚本的踩坑与复盘》
人工智能·python
通问AI2 小时前
在两台 Mac Studio 上跑 800B 模型:MLX + RDMA over Thunderbolt 实测与四道门选型法
算法
ZywOo6232 小时前
《算法竞赛进阶指南》0x03 前缀和与差分
算法
Scintilla_y2 小时前
DAY2 滑动窗口
算法·leetcode·散列表
lvwangshu2 小时前
笛卡尔树
算法·图论
import_random3 小时前
[python]conda和pip(区别和联系2)
python
Aurorar0rua3 小时前
CS50 x 2024 Notes Python - 01
开发语言·python·学习方法
026d233 小时前
状态信号只知“其然”不知“其所以然”:变工况机械故障诊断的物理盲区
算法