[LeetCode] 132 Pattern 132模式

 

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

Note: n will be less than 15,000.java

Example 1:数组

Input: [1, 2, 3, 4]

Output: False

Explanation: There is no 132 pattern in the sequence.

 

Example 2:less

Input: [3, 1, 4, 2]

Output: True

Explanation: There is a 132 pattern in the sequence: [1, 4, 2].

 

Example 3:post

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

 

这道题给咱们了一个数组,让咱们找到 132 的模式,就是第一个数小于第二第三个数,且第三个数小于第二个数。固然最直接最暴力的方法,就是遍历全部的三个数字的组合,而后验证是否知足这个规律。得莫,OJ 说打妹。那么就只能想办法去优化了,因为暴力搜索的时间复杂度是三次方,在以前的 3Sum 那道题中,咱们也有把立方的复杂度减小到平方的复杂度,至关于降了一维(降维打击么?),其实就是先固定一个数字,而后去遍历另外两个数字。咱们先肯定哪一个数字呢,固然是最小的那个啦,咱们维护一个变量 mn,初始化为整型最大值,而后在遍历数字的时候,每次用当前数字来更新 mn,而后咱们判断,若 mn 为当前数字就跳过,由于须要找到数字j的位置,数字j是大于数字i的,mn 表示的就是数字i。这样数字i和数字j都肯定了以后,就要来遍历数字k了,范围是从数组的最后一个位置到数字j之间,只要中间的任何一个数字知足题目要求的关系,就直接返回 true 便可,参见代码以下:优化

 

解法一:url

class Solution {
public:
    bool find132pattern(vector<int>& nums) {
        int n = nums.size(), mn = INT_MAX;
        for (int j = 0; j < n; ++j) {
            mn = min(mn, nums[j]);
            if (mn == nums[j]) continue;
            for (int k = n - 1; k > j; --k) {
                if (mn < nums[k] && nums[j] > nums[k]) return true;
            }
        }
        return false;
    }
};

 

那么咱们就按顺序来找这三个数,首先咱们来找第一个数,这个数须要最小,那么咱们若是发现当前数字大于等于后面一个数字,咱们就往下继续遍历,直到当前数字小于下一个数字中止。而后咱们找第二个数字,这个数字须要最大,那么若是咱们发现当前数字小于等于下一个数字就继续遍历,直到当前数字大雨下一个数字中止。最后就找第三个数字,咱们验证这个数字是否在以前两个数字的中间,若是没有找到,咱们就从第二个数字的后面一个位置继续开始从新找这三个数字,参见代码以下:spa

 

解法二:
class Solution {
public:
    bool find132pattern(vector<int>& nums) {int n = nums.size(), i = 0, j = 0, k = 0;
        while (i < n) {
            while (i < n - 1 && nums[i] >= nums[i + 1]) ++i;
            j = i + 1;
            while (j < n - 1 && nums[j] <= nums[j + 1]) ++j;
            k = j + 1;
            while (k < n) {
                if (nums[k] > nums[i] && nums[k] < nums[j]) return true;
                ++k;
            }
            i = j + 1;
        }
        return false;
    }
};    

 

下面这种方法利用单调栈来作,既简洁又高效,关于单调栈能够参见博主以前的一篇文章 LeetCode Monotonous Stack Summary 单调栈小结。思路是咱们维护一个栈和一个变量 third,其中 third 就是第三个数字,也是 pattern 132 中的2,初始化为整型最小值,栈里面按顺序放全部大于 third 的数字,也是 pattern 132 中的3,那么咱们在遍历的时候,若是当前数字小于 third,即 pattern 132 中的1找到了,咱们直接返回 true 便可,由于已经找到了,注意咱们应该从后往前遍历数组。若是当前数字大于栈顶元素,那么咱们将栈顶数字取出,赋值给 third,而后将该数字压入栈,这样保证了栈里的元素仍然都是大于 third 的,咱们想要的顺序依旧存在,进一步来讲,栈里存放的都是能够维持坐标 second > third 的 second 值,其中的任何一个值都是大于当前的 third 值,若是有更大的值进来,那就等于造成了一个更优的 second > third 的这样一个组合,而且这时弹出的 third 值比之前的 third 值更大,为何要保证 third 值更大,由于这样才能够更容易的知足当前的值 first 比 third 值小这个条件,举个例子来讲吧,好比 [2, 4, 2, 3, 5],因为是从后往前遍历,因此后三个数都不会进入 while 循环,那么栈中的数字为 5, 3, 2(其中2为栈顶元素),此时 third 仍是整型最小,那么当遍历到4的时候,终于4大于栈顶元素2了,那么 third 赋值为2,且2出栈。此时继续 while 循环,由于4仍是大于新栈顶元素3,此时 third 赋值为3,且3出栈。如今栈顶元素是5,那么 while 循环结束,将4压入栈。下一个数字2,小于 third,则找到符合要求的序列 [2, 4, 3],参见代码以下:code

 

解法三:htm

class Solution {
public:
    bool find132pattern(vector<int>& nums) {
        int third = INT_MIN;
        stack<int> st;
        for (int i = nums.size() - 1; i >= 0; --i) {
            if (nums[i] < third) return true;
            while (!st.empty() && nums[i] > st.top()) {
                third = st.top(); st.pop();
            }
            st.push(nums[i]);
        }
        return false;
    }
};

 

讨论:这道题的一个很好的 Follow up 就是求出全部 132 模式的数组,那么解法二和解法三这种想要快速来验证是否存在 132 模式的方法就不太适合,而解法一就比较适合了,咱们是须要在找到的时候不直接 return,而是将 mn,数字j,和数字k,放到一个数组里,而后加入结果 res 中,试了题目中的例子3,是能够正确的返回那三个结果的,别的也没怎么试过,感受应该是正确的。

 

参考资料:

https://leetcode.com/problems/132-pattern/

https://leetcode.com/problems/132-pattern/discuss/94135/c_ac

https://leetcode.com/problems/132-pattern/discuss/94133/Simple-java-accepted-well-explained-O(n2)-solution

https://leetcode.com/problems/132-pattern/discuss/94071/single-pass-c-on-space-and-time-solution-8-lines-with-detailed-explanation

https://leetcode.com/problems/132-pattern/discuss/94089/Java-solutions-from-O(n3)-to-O(n)-for-%22132%22-pattern-(updated-with-one-pass-slution)

 

LeetCode All in One 题目讲解汇总(持续更新中...)

相关文章
相关标签/搜索