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
相关推荐
TonyLee0178 分钟前
使用argparse模块以及shell脚本
python
Blossom.11828 分钟前
Prompt工程与思维链优化实战:从零构建动态Few-Shot与CoT推理引擎
人工智能·分布式·python·智能手机·django·prompt·边缘计算
xu_yule32 分钟前
算法基础(数论)—算法基本定理
c++·算法·算数基本定理
CoderCodingNo1 小时前
【GESP】C++五级真题(结构体排序考点) luogu-B3968 [GESP202403 五级] 成绩排序
开发语言·c++·算法
love530love2 小时前
Windows 11 下 Z-Image-Turbo 完整部署与 Flash Attention 2.8.3 本地编译复盘
人工智能·windows·python·aigc·flash-attn·z-image·cuda加速
YGGP2 小时前
【Golang】LeetCode 32. 最长有效括号
算法·leetcode
MediaTea3 小时前
Python:模块 __dict__ 详解
开发语言·前端·数据库·python
自然常数e3 小时前
字符函数和字符串函数
c语言·算法·visual studio
jarreyer3 小时前
python,numpy,pandas和matplotlib版本对应关系
python·numpy·pandas