当前位置:网站首页>leetcode:456. 132 mode [monotone stack]
leetcode:456. 132 mode [monotone stack]
2022-06-25 13:20:00 【Review of the white speed Dragon King】

analysis
We found at a glance nums[k] In the middle , Decisive analysis k
So because k Maximum index , We looked in reverse order k
Then maintain a decreasing stack in reverse order , In this way, you can find every k The next one is bigger than him nums[j]
This will satisfy kj The relationship between , Then every time a bigger one comes j, We can all update kj, bring k As big as possible
such i It is more likely to be found ( greedy ),for At the beginning of the cycle, judge if ik If the relationship is satisfied
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
summary
Monotonic stack + greedy + thinking
边栏推荐
- Restful and RPC
- QT display ffmpeg decoded pictures
- Pointer, it has to say that the subject
- Native JS --- infinite scrolling
- Golang keyboard input statement scanln scanf code example
- Using swiper to realize seamless rotation of multiple slides
- [data visualization] antv L7 realizes map visualization, drilldownlayer drill asynchronously obtains data, and suspends the warning box
- 1024 hydrology
- 深圳民太安智能二面_秋招第一份offer
- [flask tutorial] flask overview
猜你喜欢

Configuring pytorch in win10 environment

揭秘GaussDB(for Redis):全面对比Codis

剑指 Offer II 028. 展平多级双向链表

Custom vertical table

CUDA error: unspecified launch failure

Shenzhen mintai'an intelligent second side_ The first offer of autumn recruitment

《MongoDB入门教程》第01篇 MongoDB简介
![[turn] starting from the end, analyze in detail how to fill in the college entrance examination volunteer](/img/77/715454c8203d722e246ed70e1fe0d8.png)
[turn] starting from the end, analyze in detail how to fill in the college entrance examination volunteer
![[machine learning] parameter learning and gradient descent](/img/28/bb0a66b5f0702c995ca9cd7546b678.jpg)
[machine learning] parameter learning and gradient descent

关于扫雷的简易实现
随机推荐
关于数据在内存中的存储下
解析數倉lazyagg查詢重寫優化
Introduction to string 18 lectures Collection 4
Of binary tree_ Huffman tree_ Huffman encoding
Sword finger offer II 032 Effective anagrams
Heavyweight live | bizdevops: the way to break the technology situation under the tide of digital transformation
MySQL 学习笔记
Qt显示FFmpeg解码的图片
[pit avoidance means "difficult"] the antd form dynamic form is deleted, and the first line is displayed by default
数据在内存中的存储相关内容
字节跳动Dev Better技术沙龙来啦!参与活动赢好礼,限时免费报名中!
MySQL learning notes
Qt鼠标跟踪
关于猜数字游戏的实现
学习编程的起点。
Intercept based on byte length
[转]以终为始,详细分析高考志愿该怎么填
KDD 2022 | GraphMAE:自监督掩码图自编码器
[pit avoidance means "difficult"] antd select fuzzy search
Golang keyboard input statement scanln scanf code example