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].
检测:更准确地说,我们在搜索左侧有效的s1候选者时,跟踪每个有效(s2> s3)组合的s3的最大值。一旦我们遇到左边的任何数字小于我们到目前为止看到的最大s3,我们就知道我们找到了一个有效的序列,因为s1 <s3意味着s1 <s2。
 
算法:我们可以从任何一方开始,但是从右边开始可以一次扫描就做完。我们的想法是从最右边开始并搜索有效的(s2,s3)对,只需要记住最大的有效s3值,使用堆栈将有效用于此目的(堆里的所有值是s2,均大于s3)。如果左边有任何数字大于它,则数字成为s3的候选者。
 
正确性:当我们从右向左扫描时,我们可以轻松地跟踪到目前为止遇到的所有(s2,s3)候选者的最大s3值。因此,每当我们将nums [i]与区间nums [i + 1] ... nums [n-1]中s3的最大候选者进行比较时,我们实际上会问这样的问题:是否有任何132序列,其中s1 = nums [i]?因此,如果函数返回false,则必须没有132序列。
 
实现:
 
有一个堆栈,每次我们存储一个新号码时,我们首先会弹出小于该号码的所有号码。弹出的数字成为s3的候选者。
我们跟踪这样的s3的最大值(它始终是堆栈中最近弹出的数字)。
一旦我们遇到任何小于s3的数字,我们就知道我们找到了一个有效的序列,因为s1 <s3意味着s1 <s2。
RUNTIME:每个项目最多被推送一次,因此时间复杂度为O(n)。
Runtime: 24 ms, faster than 57.41% of C++ online submissions for 132 Pattern.
Memory Usage: 7.5 MB, less than 0.67% of C++ online submissions for 132 Pattern.
class Solution {
public:
bool find132pattern(vector<int>& nums) {
int third = INT32_MIN;
stack<int> s;
for(int i=nums.size()-; i>=; i--) {
if(nums[i] < third) return true;
while(!s.empty() && nums[i] > s.top()) {
third = s.top();
s.pop();
}
s.push(nums[i]);
}
return false;
}
};

LC 456. 132 Pattern的更多相关文章

  1. 456. 132 Pattern

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

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

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

  3. 【LeetCode】456. 132 Pattern

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

  4. LeetCode 456. 132 Pattern

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

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

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

  6. 456 132 Pattern 132模式

    给定一个整数序列:a1, a2, ..., an,一个132模式的子序列 ai, aj, ak 被定义为:当 i < j < k 时,ai < ak < aj.设计一个算法,当 ...

  7. [LeetCode] 132 Pattern 132模式

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

  8. Leetcode: 132 Pattern

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

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

随机推荐

  1. PAT基础级-钻石段位样卷2-7-2 吃鱼还是吃肉 (10 分)

      国家给出了 8 岁男宝宝的标准身高为 130 厘米.标准体重为 27 公斤:8 岁女宝宝的标准身高为 129 厘米.标准体重为 25 公斤. 现在你要根据小宝宝的身高体重,给出补充营养的建议. 输 ...

  2. 用js刷剑指offer(字符串的排列)

    题目描述 题目描述 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 输入 ...

  3. SLAM、三维重建,语义相关数据集大全

    作者朱尊杰,公众号:计算机视觉life,编辑成员 一 主要针对自动驾驶: 1.KITTI数据集: http://www.cvlibs.net/datasets/kitti/index.php(RGB+ ...

  4. 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...

  5. Geohash 基本知识及 .NET 下计算相邻8个区域编码

    目录 一.简介 二.计算方法 三.GeoHash的精度 四.查找相邻8个区域的Geohash编码(.NET) 五.MySQL 中使用 GeoHash 最近项目中需要搜索周边的 POI 信息,查找的过程 ...

  6. springboot+支付宝完成秒杀项目的初体验

    springboot+支付宝完成秒杀项目的初体验 思考的问题: 首先是秒杀的商品查询,考虑到是热点数据,所以写一个接口读取当日批次的秒杀商品到redis中(那么接下来对商品的操作都放入redis中). ...

  7. machine learning(15) --Regularization:Regularized logistic regression

    Regularization:Regularized logistic regression without regularization 当features很多时会出现overfitting现象,图 ...

  8. 算法:用户喜好--Map与List配合下的查找

    提示:在算法处理过程中,未必就要将出现在前面的作为关键字检索.比如本题,非得先去检索范围,再去判断范围中key的个数.反其道而行,把输入的数字当作关键字,组成Map package test; imp ...

  9. vue router.beforeEach(),详解

    outer.beforeEach()一般用来做一些进入页面的限制. 比如没有登录, 就不能进入某些页面,只有登录了之后才有权限查看某些页面...说白了就是路由拦截.第一步 规定进入路由需不需要权限 @ ...

  10. BUG----spark

    我也想说很尴尬, 搞到显现 发现 原来是个版本的bug spark 1.6.0  有个BUG 希望更多人看到 Traceback (most recent call last):  File &quo ...