当前位置:网站首页>Leetcode 456.132 mode monotone stack /medium
Leetcode 456.132 mode monotone stack /medium
2022-07-27 15:27:00 【Abcheche】
List of articles
1.Description
Give you an array of integers nums , There are... In the array n It's an integer .132 Subsequences of patterns By three integers nums[i]、nums[j] and nums[k] form , And meet :i < j < k and nums[i] < nums[k] < nums[j] .
If nums in 132 Subsequences of patterns , return true ; otherwise , return false .
2.Example
Input :nums = [3,1,4,2]
Output :true
explain : There is... In the sequence 1 individual 132 Subsequences of patterns : [1, 4, 2] .
3.Solution
Maintain a monotone stack ( From the top of the stack to the bottom of the stack is monotonically increasing or decreasing , Here, from the top of the stack to the bottom of the stack is increasing ), send k Always the second lowest value , When a smaller value is found k A small number of hours shows that there is an answer .
public boolean find132pattern(int[] nums) {
Deque<Integer> stack = new ArrayDeque<Integer>();
int n = nums.length;
if(n<3) {
return false;
}
int k = -1000000000;
for(int i=n-1;i>=0;i--) {
// When the smaller value is found k When the number is small , It means that i(k It's a small , The stack must be full of comparisons k Big )
if(nums[i]<k) {
return true;
}
// The stack is all from i To the end of the array k Big elements ( And it is in descending order in the stack ), When you encounter an element larger than the top of the stack , maintain k Is the second smallest value , Assign the top element of the stack to k Then add elements .
while(!stack.isEmpty()&&stack.peekLast()<nums[i]) {
k = stack.removeLast();
}
stack.addLast(nums[i]);
}
return false;
}
边栏推荐
- 网络设备硬核技术内幕 路由器篇 17 DPDK及其前传(二)
- MySQL interview 40 consecutive questions, interviewer, if you continue to ask, I will turn my face
- Do you really understand CMS garbage collector?
- Zhou Hongyi: if the digital security ability is backward, it will also be beaten
- 多表查询_子查询概述和多表查询_子查询情况1&情况2&情况3
- Reading notes of lifelong growth (I)
- 网络设备硬核技术内幕 路由器篇 7 汤普金森漫游网络世界(下)
- EMC design scheme of USB2.0 Interface
- Basic usage of kotlin
- Network equipment hard core technology insider router Chapter 5 tompkinson roaming the network world (Part 1)
猜你喜欢

基于stm32的数字示波器设计方案

仅做两项修改,苹果就让StyleGANv2获得了3D生成能力

AssetBundle如何打包

CAN总线的EMC设计方案
Principle of MOS tube to prevent reverse connection of power supply

What is the breakthrough point of digital transformation in the electronic manufacturing industry? Lean manufacturing is the key

Unity performance optimization ----- LOD (level of detail) of rendering optimization (GPU)

What is tor? What is the use of tor browser update?

谷粒商城配置CorsWebFilter后,报错:Resource sharing error:MultipleAllowOriginValues

移动端使用vantUI的list组件,多个tab项来回切换时,列表加载多次导致数据无法正常展示
随机推荐
LeetCode 781. 森林中的兔子 哈希表/数学问题 medium
Basic usage of kotlin
How to edit a framework resource file separately
两阶段提交与三阶段提交
《剑指Offer》剪绳子
修改frameworks资源文件如何单编
Unity3D学习笔记10——纹理数组
The reverse order pairs in the "sword finger offer" array
Leetcode-1737- minimum number of characters to change if one of the three conditions is met
lua学习笔记
Tools - common methods of markdown editor
Kubernetes CNI classification / operation mechanism
The first common node of the two linked lists of "Jianzhi offer"
网络设备硬核技术内幕 路由器篇 3 贾宝玉梦游太虚幻境 (中)
2022-07-27日报:IJCAI 2022杰出论文公布,大陆作者中稿298篇拿下两项第一
Stm32f103c8t6 drives sh1106 1.3 "IIC OLED display under Arduino frame
《终身成长》读书笔记(一)
Unity 鼠标控制第一人称摄像机视角
STM32F10x_硬件I2C读写EEPROM(标准外设库版本)
STM32 can -- can ID filter analysis