zoukankan      html  css  js  c++  java
  • 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 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

    我在它基础上稍作改动

     1 public class Solution {
     2     public class Pair {
     3         int min;
     4         int max;
     5         public Pair(int n1, int n2) {
     6             min = n1;
     7             max = n2;
     8         }
     9     }
    10     
    11     public boolean find132pattern(int[] nums) {
    12         if (nums.length < 3) return false;
    13         Stack<Pair> st = new Stack<Pair>();
    14         for (int n : nums) {
    15             if (st.isEmpty() || n<=st.peek().min) st.push(new Pair(n, n));
    16             else {
    17                 if (n < st.peek().max) return true;
    18                 Pair last = st.pop();
    19                 last.max = Math.max(last.max, n);
    20                 while (!st.isEmpty() && last.max>=st.peek().max) st.pop();
    21                 if (!st.isEmpty() && last.max>st.peek().min) return true;
    22                 st.push(last);
    23             }
    24         }
    25         return false;
    26     }
    27 }
  • 相关阅读:
    iMX6Q开发板的EIM接口的配置可以与FPGA通讯-交换数据-最常用的接口配置
    大受喜欢安卓触控一体机连接云端数据化管理提供例程DEMO
    iTOP-4418开发板所用核心板研发7寸/10.1寸安卓触控一体机
    i.MX6ULL终结者Buildoot文件系统构建篇buildroot添加支持第三方软件
    所有教程为迅为原创-3399开发板资料更新了
    如果使用4412开发板那么怎么搭建和测试TFTP服务器
    iMX6ULL开发板Linux 4G通信实验EC20 4G模块配置
    iMX6ULL终结者Linux WIFI驱动实验rtl8723 Wifi联网测试
    iTOP4412开发板Android5.1.1移植教程
    迅为iTOP3399开发板人工智能(图像分类)
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/6163128.html
Copyright © 2011-2022 走看看