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
相关推荐
阿崽meitoufa17 分钟前
JVM虚拟机:垃圾收集器和判断对象是否存活的算法
java·jvm·算法
ballball~~1 小时前
拉普拉斯金字塔
算法·机器学习
Cemtery1161 小时前
Day26 常见的降维算法
人工智能·python·算法·机器学习
星空椰2 小时前
快速掌握FastAPI:高效构建Web API
python·fastapi
塔尖尖儿2 小时前
Python中range()到底是什么演示
python
Ethan-D3 小时前
#每日一题19 回溯 + 全排列思想
java·开发语言·python·算法·leetcode
Benny_Tang3 小时前
题解:CF2164C Dungeon
c++·算法
仙俊红3 小时前
LeetCode174双周赛T3
数据结构·算法
橘颂TA3 小时前
【剑斩OFFER】算法的暴力美学——LeetCode 733 题:图像渲染
算法·leetcode·职场和发展