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
相关推荐
happylifetree8 分钟前
Python79:核心语法-面向对象基础-实例方法
python
加宝的起点雷达1 小时前
40 条候选里 38 条来自同一个源,主卡整屏重样
人工智能·算法·创业
贝壳6901 小时前
bool-hybrid-array 9.13.6 偷偷加了个"按键自动升序"的字典,我翻出来测了下
python
秋天的怀念_David1 小时前
算法一千零一夜 · 第二十六夜 协议的母语
java·spring boot·算法
艺杯羹1 小时前
MCP本地服务集成避坑:从Stdio管道僵死到双向JSON-RPC稳健通信
人工智能·python·ai·系统架构·大语言模型·ai编程·mcp
happylifetree1 小时前
Python78:核心语法-面向对象基础-类与对象
python
duansamve1 小时前
Python中进程
python
文丑颜不良啊2 小时前
LeetCode2021~2040题目解析
java·leetcode
日日大王学编程2 小时前
AI Agent 技术发展历程、演进逻辑与技术范式综述
人工智能·python