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
相关推荐
you鬰4 分钟前
datawhale--llm-algo-leetcode5️⃣
python·datawhale
老洋葱Mr_Onion17 分钟前
【C++】CSP-J初赛模拟卷七错题整理(作者自用)
c++·算法·深度优先
λqaq724 分钟前
MySQL 数据库基础(2)约束、用户权限、远程连接、外键与 TCP/UDP 理解
linux·数据库·python·tcp/ip·mysql
人工干智能27 分钟前
科普:pandas 时间偏移别名:ts.resample(“5Min“).sum()
python·pandas
Nil20830 分钟前
leetcode 114二叉树展开为链表
leetcode·链表·深度优先
开源量化GO37 分钟前
最新AI辅助量化表达:先理清规则,再按需求选工具
人工智能·python
土星云SaturnCloud39 分钟前
Real-ESRGAN超分辨率算法原理与边缘侧部署实践
服务器·算法·ai·边缘计算·real-esrgan
cz071042 分钟前
hot100_搜索二维矩阵 II
算法·leetcode
Zane199443 分钟前
为什么 del 一个对象有时立刻消失,有时却纹丝不动?一文讲透引用计数与垃圾回收
后端·python
yichudu1 小时前
Python 装饰器 decorator
开发语言·python