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 ...
随机推荐
- 【noiOJ】p7940
01:查找最接近的元素 总时间限制: 1000ms 内存限制: 65536kB 描述 在一个非降序列中,查找与给定值最接近的元素. 输入 第一行包含一个整数n,为非降序列长度.1 <= n ...
- js小效果-简易计算器
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...
- Makefile学习
makefile中常用的函数: http://linux.chinaunix.net/techdoc/develop/2009/07/09/1122854.shtml SRC = $(wildcard ...
- 简单 常用的git命令
常用的git命令 git pull 获取最新 git add . 提交所有 git commit -m “我的注释” git status 查看状态 git push origin master ...
- 深入浅出 - Android系统移植与平台开发(六)- 为Android启动加速
作者:唐老师,华清远见嵌入式学院讲师. Android的启动速度一直以来是他的诟病,虽然现在Android设备的硬件速度越来越快,但是随着新 版本的出现,其启动速度一直都比较慢,当然,作为程序员,我们 ...
- 在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke
今天关闭一个窗体,报出这样的一个错误"在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke.",这个不用多想,肯定是那个地方没有释放掉.既然碰到这个问题, ...
- Shell脚本中执行mysql的几种方式(转)
Shell脚本中执行mysql的几种方式(转) 对于自动化运维,诸如备份恢复之类的,DBA经常需要将SQL语句封装到shell脚本.本文描述了在Linux环境下mysql数据库中,shell脚本下调用 ...
- php归档函数(按时间)实现
今日开发本站需要用到按时间归档文章的功能,即按文档发布时间将文章文类,以实现检索和统计功能,于是自己写了一个, 现分享给大家,相信大家工作和学习中有可能会用到,实现原理很简单,即取出文章发布时间戳的年 ...
- 2015Web前端攻城之路
2015目标成为一名合格的前端攻城狮. 养成计划: 1.html / css 2.js 3.ajax 4.框架 5.项目实战
- 《深入.NET平台和C# 编程》内测纠错记录
1. .NET框架的核心组件包括(BD)(选择两项) A.CTS (通用类型系统) B.CLR (公共语言运行时,.NET的基础) C.CLS (公共语言规范) D.FCL (框架类 ...