当前位置:网站首页>leetcode:456. 132 模式【单调栈】
leetcode:456. 132 模式【单调栈】
2022-06-25 12:35:00 【白速龙王的回眸】

分析
我们一眼发现nums[k]在中间,果断分析k
因此由于k索引最大,我们倒序找k
然后倒序来维护一个递减栈,这样就可以找到离每个k最近的下一个比他大的nums[j]
这样就可以满足kj的关系,然后每次来了更大的j,我们都可以更新kj,使得k尽量大
这样i可找到的可能性就更大(贪心),for循环一开始判断如果ik关系满足即可
ac code
class Solution:
def find132pattern(self, nums: List[int]) -> bool:
# nums[k] is in the middle
# we consider k
n = len(nums)
v_k = -inf # nums[k]
st = []
# reverse and next bigger than myself
for i in range(n - 1, -1, -1):
if nums[i] < v_k:
return True
# make sure there is a j s.t. nums[j] > nums[k] while j < k
while st and st[-1] < nums[i]: # nums[k] < nums[j], make nums[k] as big as possible(greedy), so that it can find i easier
v_k = st.pop()
st.append(nums[i]) # keep a decreasing stack
return False
总结
单调栈 + 贪心 + 思维
边栏推荐
- [data visualization] antv L7 realizes map visualization, drilldownlayer drill asynchronously obtains data, and suspends the warning box
- 字符串入门十八讲合集四
- golang键盘输入语句scanln scanf代码示例
- MySQL adds, modifies, and deletes table fields, field data types, and lengths (with various actual case statements)
- Reload cuda/cudnn/pytorch
- Common colors for drawing
- 揭秘GaussDB(for Redis):全面对比Codis
- About data storage in memory
- Native JS --- infinite scrolling
- 康威定律,作为架构师还不会灵活运用?
猜你喜欢
![[flask tutorial] flask overview](/img/e8/d85ac54f3a9eb3b1ab31852761154c.jpg)
[flask tutorial] flask overview

About data storage in memory

leetcode - 384. 打乱数组

剑指 Offer II 025. 链表中的两数相加

CUDA error: unspecified launch failure
![[data visualization] 360 ° teaching you how to comprehensively learn visualization - Part 1](/img/36/167397ce61240036c865dd99463f1b.jpg)
[data visualization] 360 ° teaching you how to comprehensively learn visualization - Part 1

爱可可AI前沿推介(6.25)

《MongoDB入门教程》第01篇 MongoDB简介

mysql导入导出数据到excel表日期出现问题

剑指 Offer II 032. 有效的变位词
随机推荐
Assemble relevant knowledge points of flag bit (connected)
Conway's law can not be flexibly applied as an architect?
药物设计新福音:腾讯联合中科大、浙大开发自适应图学习方法,预测分子相互作用及分子性质
解析數倉lazyagg查詢重寫優化
LeetCode链表题解技巧归纳总结
And console Log say goodbye
指针,它那些不得不说的题目
. NET in China - What's New in . NET
[visio] solving the fuzzy problem of parallelogram in word
Sword finger offer II 025 Adding two numbers in a linked list
KVM 脚本管理 —— 筑梦之路
Maui's learning path (II) -- setting
Analyse de l'optimisation de la réécriture des requêtes lazyagg de l'entrepôt
关于结构体,枚举,联合的一些知识
An article clearly explains MySQL's clustering / Federation / coverage index, back to table, and index push down
nacos无法修改配置文件Mysql8.0的解决方法
深圳民太安智能二面_秋招第一份offer
Back test of quantitative trading - tqzfuturerenkowavestrategy
Jenkins pipeline uses
字符串入门十八讲合集四