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
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.
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 ...
随机推荐
- UICollectionView集合视图的概念
如何创建UICollectionView 集合视图的布局UICollectionViewFlowLayout 自定义cell 布局协议UICollectionViewDelegateFlowLayou ...
- 开源工作流引擎CCFlow 学习专区
http://bbs.ccflow.org/index.aspx 官网:http://www.ccflow.org/
- Ajax注册验证js代码
分享jquery网站:http://www.css88.com/jqapi-1.9/focusout/ $(document).ready(function() { var bool_user = f ...
- POI Word 模板 文字 图片 替换
实验环境:POI3.7+Word2007 Word模板: 替换后效果: 代码: 1.入口文件 public class Test { public static void main(String[] ...
- 360浏览器Uncaught TypeError: object is not a function问题
刚刚360浏览器提示 Uncaught TypeError: object is not a function,找了半天发现问题是我有一个按钮,id和方法重复了,所以提示这个. <input t ...
- GUID相关知识。。。。转载
全局唯一标识符(GUID,Globally Unique Identifier)是一种由算法生成的二进制长度为128位的数字标识符.GUID主要用于在拥有多个节点.多台计算机的网络 ...
- Mysql event学习
我们可能比较熟悉crond,但是mysql也有一个自己的叫event,oracle的叫job. 开启mysql的event有很多种方法,和临时开启.我们在配置文件里面添加参数,随着服务一起开启. 在[ ...
- php连接多数据库
<?php $conn1=mysql_connect('localhost','root','','new_link '); $conn2=mysql_connect('localhost',' ...
- JDBC链接oracle已经mysql的测试
1.链接oracle package cn.itcast.mybatis.dao; import java.sql.Connection;import java.sql.DriverManager;i ...
- pch头文件
1.command+N ---> Other ---> PCH File 2.点击工程 ---> Build Settings ---> 搜索框中输入pref ---> ...