456. 132 Pattern**
来源:互联网 发布:黄伟文十大经典知乎 编辑:程序博客网 时间:2024/06/01 22:20
Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that i < j < k and ai < ak < aj. Design an algorithm that takes a list of n numbers as input and checks whether there is a 132 pattern in the list.
Note: n will be less than 15,000.
Example 1:
Input: [1, 2, 3, 4]Output: FalseExplanation: There is no 132 pattern in the sequence.
Example 2:
Input: [3, 1, 4, 2]Output: TrueExplanation: There is a 132 pattern in the sequence: [1, 4, 2].
Example 3:
Input: [-1, 3, 2, 0]Output: TrueExplanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].
The idea is that we can use a stack to keep track of previous min-max intervals.
Here is the principle to maintain the stack:
For each number num
in the array
If stack is empty:
- push a new Pair of
num
into stack
If stack is not empty:
if
num
<stack.peek().min
, push a new Pair ofnum
into stackif
num
>=stack.peek().min
, we first pop() out the peek element, denoted aslast
if
num
<last.max
, we are done, returntrue
;if
num
>=last.max
, we mergenum
intolast
, which meanslast.max
=num
.
Once we updatelast
, if stack is empty, we just push backlast
.
However, the crucial part is:
If stack is not empty, the updatedlast
might:- Entirely covered stack.peek(), i.e.
last.min
<stack.peek().min
(which is always true) &&last.max
>=stack.peek().max
, in which case we keep popping out stack.peek(). - Form a 1-3-2 pattern, we are done ,return
true
- Entirely covered stack.peek(), i.e.
So at any time in the stack, non-overlapping Pairs
are formed in descending order by their min value, which means the min value of peek element in the stack is always the min value globally.
class Pair{ int min, max; public Pair(int min, int max){ this.min = min; this.max = max; } } public boolean find132pattern(int[] nums) { Stack<Pair> stack = new Stack(); for(int n: nums){ if(stack.isEmpty() || n <stack.peek().min ) stack.push(new Pair(n,n)); else if(n > stack.peek().min){ Pair last = stack.pop(); if(n < last.max) return true; else { last.max = n; while(!stack.isEmpty() && n >= stack.peek().max) stack.pop(); // At this time, n < stack.peek().max (if stack not empty) if(!stack.isEmpty() && stack.peek().min < n) return true; stack.push(last); } } } return false; }
总结:关键在于stack.peek() min最小,max最大
- LeetCode 456. 132 Pattern
- 456. 132 Pattern
- Leetcode-456. 132 Pattern
- LeetCode 456. 132 Pattern
- 456. 132 Pattern
- 456. 132 Pattern
- 456. 132 Pattern**
- Leetcode-456. 132 Pattern
- LeetCode 456. 132 Pattern
- Leetcode 456. 132 Pattern
- 456. 132 Pattern
- [LeetCode]456. 132 Pattern
- leetcode 456. 132 Pattern
- 456. 132 Pattern
- LeetCode 456. 132 Pattern
- 456. 132 Pattern
- [leetcode]456. 132 Pattern
- 456. 132 Pattern
- mysqlbakcup的安装
- PAT乙级-1005. 继续(3n+1)猜想
- 十六周练习--计算该日在本年中是第几天
- POJ 3068 "Shortest" pair of paths 已翻译
- 十种常见排序算法
- 456. 132 Pattern**
- 三十九讲项目1.1——输入一个数n,判断n是否是完数
- Gearmand学习3:Plugin插件
- 获取Android联系人最简洁的代码
- HDU2015 偶数求和
- 【分布式】Zookeeper数据与存储
- 1613-3-傅溥衍 总结《2016年12月17日》【连续第七十八天总结】
- js 》》判断xx(图片等)是否加载成功的方法 》》》》》》》》》打飞机中的代码块
- Android重温--文件存储