当前位置:网站首页>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
总结
单调栈 + 贪心 + 思维
边栏推荐
- Which Chinese virtual human is better? Sullivan, IDC: Xiaobing Baidu Shangtang ranks in the first echelon
- Django框架——缓存、信号、跨站请求伪造、 跨域问题、cookie-session-token
- [AI helps scientific research] fool drawing of loss curve
- KDD 2022 | GraphMAE:自监督掩码图自编码器
- Sword finger offer II 028 Flatten multi-level bidirectional linked list
- 德国举行全球粮食安全团结会议
- MySQL 学习笔记
- 1024水文
- [data visualization] 360 ° teaching you how to comprehensively learn visualization - Part 1
- JVM参数解释
猜你喜欢

Conway's law can not be flexibly applied as an architect?

MySQL adds, modifies, and deletes table fields, field data types, and lengths (with various actual case statements)

Which Chinese virtual human is better? Sullivan, IDC: Xiaobing Baidu Shangtang ranks in the first echelon

Capabilities required by architects

爱可可AI前沿推介(6.25)

J2EE从入门到入土01.MySQL安装

Solution to Nacos' failure to modify the configuration file mysql8.0

Meichuang was selected into the list of "2022 CCIA top 50 Chinese network security competitiveness"

Update PIP & Download jupyter Lab

數據在內存中的存儲相關內容
随机推荐
剑指 Offer II 032. 有效的变位词
字符串各操作函数与内存函数详解
JVM parameter interpretation
中国虚拟人哪家强?沙利文、IDC:小冰百度商汤位列第一梯队
Optimization of lazyagg query rewriting in parsing data warehouse
Fedora 35 deploys DNS master-slave and separation resolution -- the way to build a dream
Sword finger offer II 029 Sorted circular linked list
[pit avoidance means "difficult"] the antd form dynamic form is deleted, and the first line is displayed by default
剑指 Offer 04. 二维数组中的查找
KDD 2022 | GraphMAE:自监督掩码图自编码器
My first experience of go+ language -- a collection of notes on learning go+ design architecture
À propos du stockage des données en mémoire
. NET in China - What's New in . NET
[pit avoidance means "difficult"] actionref current. Reload() does not take effect
三行代码简单修改jar包的项目代码
指针,它那些不得不说的题目
Back test of quantitative trading - example of futures CTA strategy (tqzfuturerenkoscalpingstrategy)
Qt显示FFmpeg解码的图片
Uncover gaussdb (for redis): comprehensive comparison of CODIS
Reload cuda/cudnn/pytorch