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 of num into stack

    • if num >= stack.peek().min, we first pop() out the peek element, denoted as last

      • if num < last.max, we are done, return true;

      • if num >= last.max, we merge num into last, which means last.max = num.
        Once we update last, if stack is empty, we just push back last.
        However, the crucial part is:
        If stack is not empty, the updated last 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

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的更多相关文章

  1. [LeetCode] 132 Pattern 132模式

    Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that  ...

  2. 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 ...

  3. 【LeetCode】456. 132 Pattern 解题报告(Python)

    [LeetCode]456. 132 Pattern 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  4. 【LeetCode】456. 132 Pattern

    Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that  ...

  5. LeetCode 456. 132 Pattern

    问题描述 给一组数,判断这组数中是否含有132 pattern. 132 pattern: i < j < k, 且 ai < ak < aj 第一种解法 使用栈来保存候选的子 ...

  6. [leetcode] 456. 132 Pattern (Medium)

    对一个三个元素以上的数组,如果存在1-3-2模式的组合,则返回true. 1-3-2模式就是值的排序是i<k<j但是下标排序是i<j<k. 解法一: 硬解,利用一个变量存储是否 ...

  7. 456. 132 Pattern

    456. 132 Pattern Given an array of integers a1, a2, a3-an, judge if there exists the 132 pattern. 13 ...

  8. [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  ...

  9. LC 456. 132 Pattern

    Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that  ...

随机推荐

  1. Codeforces Round #210 (Div. 2) C. Levko and Array Recovery

    题目链接 线段树的逆过程,想了老一会,然后发现应该是包含区间对存在有影响,就不知怎么做了...然后尚大神,说,So easy,你要倒着来,然后再正着来,判断是不是合法就行了.然后我乱写了写,就过了.数 ...

  2. HDU 1700 Points on Cycle(向量旋转)

    题目链接 水题,卡了下下精度. #include <cstdio> #include <iostream> #include <cmath> using names ...

  3. ASP.NET状态保持方案若干

    客户端方案: 1.ViewState 2.隐藏域 3.cookie 大小4KB限制,不消耗服务器资源,可配置到期时间,但安全性不高,还被客户端禁用. 4.QueryString 方法简单,但不安全,有 ...

  4. BZOJ4546: codechef XRQRS

    Description 给定一个初始时为空的整数序列(元素由1开始标号)以及一些询问: 类型1:在数组后面就加入数字x. 类型2:在区间L…R中找到y,最大化(x xor y). 类型3:删除数组最后 ...

  5. BZOJ1485: [HNOI2009]有趣的数列

    Description 我们称一个长度为2n的数列是有趣的,当且仅当该数列满足以下三个条件: (1)它是从1到2n共2n个整数的一个排列{ai}: (2)所有的奇数项满足a1<a3<…&l ...

  6. Linux任务调度命令(轻松管理Linux)

    Linux任务调度其实就是让系统在某个时间执行某些命令或者程序,这样可以让管理员更加轻松地管理自己的Linux,当我刚了解到这个方法时,我的内心充满了无尽的欣喜,感觉Linux实在是太强大了. 下面我 ...

  7. JSP 页面缓存以及清除缓存

    一.概述 缓存的思想可以应用在软件分层的各个层面.它是一种内部机制,对外界而言,是不可感知的. 数据库本身有缓存,持久层也可以缓存.(比如:hibernate,还分1级和2级缓存) 业务层也可以有缓存 ...

  8. 防止系统内存溢出触发OOM的一个内核参数

    [root@djf_dev_server ~]# sysctl -a|grep overcomvm.overcommit_memory = 0 0 内存不足报错(不会继续分配内存,防止oom)1 表示 ...

  9. 状态压缩 DP

    D - Hie with the Pie Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:65536 ...

  10. ArcGIS JavaScript API异常之onExtentChange事件覆盖onClick事件

    利用Esri官方提供的聚合类进行聚合,由于数据较多,为了加快速度,在聚合之前对当期范围进行判断,如果不在当前视图范围的,就不聚合了. 所以,由于Esri官方的类是监听了zoomEnd事件,如下代码 t ...