Leetcode: 132 Pattern
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: False Explanation: There is no 132 pattern in the sequence.
Example 2:
Input: [3, 1, 4, 2] Output: True Explanation: There is a 132 pattern in the sequence: [1, 4, 2].
Example 3:
Input: [-1, 3, 2, 0] Output: True Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].
我觉得这道题是hard,难点第一是要想到用stack,第二是要维护一个这样子的min-max序列: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.
The idea is that we can use a stack to keep track of previous min-max intervals.
For each number num in the array
If stack is empty:
- push a new Pair of
numinto stack
If stack is not empty:
if
num<stack.peek().min, push a new Pair ofnuminto stackif
num>=stack.peek().min, we first pop() out the peek element, denoted aslastif
num<last.max, we are done, returntrue;if
num>=last.max, we mergenumintolast, 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 updatedlastmight:- 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.
refer to: https://discuss.leetcode.com/topic/68193/java-o-n-solution-using-stack-in-detail-explanation/2
我在它基础上稍作改动
public class Solution {
public class Pair {
int min;
int max;
public Pair(int n1, int n2) {
min = n1;
max = n2;
}
}
public boolean find132pattern(int[] nums) {
if (nums.length < 3) return false;
Stack<Pair> st = new Stack<Pair>();
for (int n : nums) {
if (st.isEmpty() || n<=st.peek().min) st.push(new Pair(n, n));
else {
if (n < st.peek().max) return true;
Pair last = st.pop();
last.max = Math.max(last.max, n);
while (!st.isEmpty() && last.max>=st.peek().max) st.pop();
if (!st.isEmpty() && last.max>st.peek().min) return true;
st.push(last);
}
}
return false;
}
}
Leetcode: 132 Pattern的更多相关文章
- [LeetCode] 132 Pattern 132模式
Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that ...
- LeetCode解题报告—— 1-bit and 2-bit Characters & 132 Pattern & 3Sum
1. 1-bit and 2-bit Characters We have two special characters. The first character can be represented ...
- 【LeetCode】456. 132 Pattern 解题报告(Python)
[LeetCode]456. 132 Pattern 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 【LeetCode】456. 132 Pattern
Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that ...
- LeetCode 456. 132 Pattern
问题描述 给一组数,判断这组数中是否含有132 pattern. 132 pattern: i < j < k, 且 ai < ak < aj 第一种解法 使用栈来保存候选的子 ...
- [leetcode] 456. 132 Pattern (Medium)
对一个三个元素以上的数组,如果存在1-3-2模式的组合,则返回true. 1-3-2模式就是值的排序是i<k<j但是下标排序是i<j<k. 解法一: 硬解,利用一个变量存储是否 ...
- 456. 132 Pattern
456. 132 Pattern Given an array of integers a1, a2, a3-an, judge if there exists the 132 pattern. 13 ...
- [Swift]LeetCode456. 132模式 | 132 Pattern
Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that ...
- LC 456. 132 Pattern
Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that ...
随机推荐
- 【Oracle】同义词问题
优点: 1.节省数据库空间,多用户可以操作同一张表: 2.扩展的数据库的使用范围,能够在不同的数据库用户之间实现无缝交互: 3.利用Database Link.创建同义词可以实现不同数据库服务器之间的 ...
- HDU 2089 简单数位dp
1.HDU 2089 不要62 简单数位dp 2.总结:看了题解才敲出来的,还是好弱.. #include<iostream> #include<cstring> #i ...
- Linux_CentOS6.5安装vncserver实现图形化访问
一. 安装gnome图形化桌面 #yum groupinstall -y "X Window System" #yum groupinstall -y "Desktop& ...
- Cloudera Manager 5和CDH5离线安装
CDH (Cloudera’s Distribution, including Apache Hadoop),是Hadoop众多分支中的一种,由Cloudera维护,基于稳定版本的Apache Had ...
- C语言-结构体
#include<stdio.h> struct stu //定义结构体类型 { int num; char *name; char *sex; float score; } boy[]= ...
- mysql 存储过程 删除重复
DELIMITER $$ CREATE PROCEDURE `delRepeatCA`() BEGIN DECLARE tally INT DEFAULT 0; SELECT COUNT(rs.c_C ...
- E: dpkg 被中断,您必须手工运行 sudo dpkg --configure -a 解决此问题。
学习 : http://blog.csdn.net/darennet/article/details/9009361 http://www.uedsc.com/dpkg-sudo-dpkg-confi ...
- javascript 变量,作用域,内存管理小结
js的变量保存两种类型的数据——基本数据类型与引用类型.具有以下几点特征: 变量: 1)基本类型值在内存中占固定大小的空间,因此被保存在栈内存中; 2) 把保存基本类型值得变量赋给另一个变量,会创 ...
- HDU1134/HDU1133 递推 大数 java
Game of Connections Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- IE 9 以下兼容HTML5
<head> <meta name="viewport" content="width=device-width,initial-scale=1.0&q ...