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
相关推荐
love530love几秒前
技术复盘:llama-cpp-python CUDA 编译实战 (Windows)
人工智能·windows·python·llama·aitechlab·cpp-python·cuda版本
睡一觉就好了。6 分钟前
快速排序——霍尔排序,前后指针排序,非递归排序
数据结构·算法·排序算法
Tansmjs35 分钟前
C++编译期数据结构
开发语言·c++·算法
金枪不摆鳍36 分钟前
算法-字典树
开发语言·算法
diediedei41 分钟前
C++类型推导(auto/decltype)
开发语言·c++·算法
逄逄不是胖胖1 小时前
《动手学深度学习》-60translate实现
人工智能·python·深度学习
橘颂TA1 小时前
【测试】自动化测试函数介绍——web 测试
python·功能测试·selenium·测试工具·dubbo
爱学习的阿磊1 小时前
Python上下文管理器(with语句)的原理与实践
jvm·数据库·python
m0_736919101 小时前
Python面向对象编程(OOP)终极指南
jvm·数据库·python
one____dream1 小时前
【网安】Reverse-非常规题目
linux·python·安全·网络安全·ctf