当前位置:网站首页>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;
}
边栏推荐
- 《剑指Offer》 合并两个排序的链表
- Photoelectric isolation circuit design scheme (six photoelectric isolation circuit diagrams based on optocoupler and ad210an)
- STM32之CAN ---CAN ID过滤器分析
- 仅做两项修改,苹果就让StyleGANv2获得了3D生成能力
- 2022-07-27日报:IJCAI 2022杰出论文公布,大陆作者中稿298篇拿下两项第一
- Data warehouse project is never a technical project
- LeetCode 81. 搜索旋转排序数组 II 二分/medium
- Unity performance optimization ----- occlusion culling of rendering optimization (GPU)
- JUC(JMM、Volatile)
- 基于stm32的数字示波器设计方案
猜你喜欢

LeetCode 190. 颠倒二进制位 位运算/easy

Unity mouse controls the first person camera perspective
仪表放大器和运算放大器优缺点对比

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

LeetCode 面试题 17.21. 直方图的水量 双指针,单调栈/hard

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

Digital storage oscilloscope based on FIFO idt7202-12
MOS管防止电源反接的原理

Watermelon book machine learning reading notes Chapter 1 Introduction

Kubernetes CNI classification / operation mechanism
随机推荐
Design scheme of digital oscilloscope based on stm32
适配验证新职业来了!华云数据参与国家《信息系统适配验证师国家职业技能标准》编制
Huayun data creates a perfect information technology and innovation talent training system to help the high-quality development of information technology and innovation industry
Wechat applet realizes music search page
周鸿祎:数字安全能力落后也会挨打
After configuring corswebfilter in grain mall, an error is reported: resource sharing error:multiplealloworiginvalues
Comparison of advantages and disadvantages between instrument amplifier and operational amplifier
Principle of MOS tube to prevent reverse connection of power supply
Problem solving in magic tower project
Four kinds of relay schemes driven by single chip microcomputer
网络设备硬核技术内幕 路由器篇 18 DPDK及其前传(三)
Unity performance optimization ----- LOD (level of detail) of rendering optimization (GPU)
仅做两项修改,苹果就让StyleGANv2获得了3D生成能力
MySQL interview 40 consecutive questions, interviewer, if you continue to ask, I will turn my face
网络设备硬核技术内幕 路由器篇 6 汤普金森漫游网络世界(中)
Selenium 报错:session not created: This version of ChromeDriver only supports Chrome version 81
分布式锁
谷粒商城配置CorsWebFilter后,报错:Resource sharing error:MultipleAllowOriginValues
Several basic uses of tl431-2.5v voltage reference chip
USB interface electromagnetic compatibility (EMC) solution