132pattern-Leetcode456
QUESTION: To search for a subsequence (s1,s2,s3) such that s1 < s3 < s2.
INTUITION: Suppose we want to find a 123 sequence with s1 < s2 < s3, we just need to find s3, followed by s2 and s1. Now if we want to find a 132 sequence with s1 < s3 < s2, we need to switch up the order of searching. we want to first find s2, followed by s3, then s1.
DETECTION: More precisely, we keep track of highest value of s3 for each valid (s2 > s3) pair while searching for a valid s1 candidate to the left. Once we encounter any number on the left that is smaller than the largest s3 we have seen so far, we know we found a valid sequence, since s1 < s3 implies s1 < s2.
ALGORITHM: We can start from either side but I think starting from the right allow us to finish in a single pass. The idea is to start from end and search for valid (s2,s3) pairs, we just need to remember the largest valid s3 value, using a stack will be effective for this purpose. A number becomes a candidate for s3 if there is any number on the left bigger than it.
CORRECTNESS: As we scan from right to left, we can easily keep track of the largest s3value of all (s2,s3) candidates encountered so far. Hence, each time we compare nums[i] with the largest candidate for s3 within the interval nums[i+1]...nums[n-1] we are effectively asking the question: Is there any 132 sequence with s1 = nums[i]?Therefore, if the function returns false, there must be no 132 sequence.
IMPLEMENTATION:
- Have a
stack, each time we store a new number, we firstpopout all numbers that are smaller than that number. The numbers that arepoppedout becomes candidate fors3. - We keep track of the
maximumof suchs3(which is always the most recentlypoppednumber from thestack). - Once we encounter any number smaller than
s3, we know we found a valid sequence sinces1 < s3impliess1 < s2.
132pattern-Leetcode456的更多相关文章
- 132-pattern(蛮难的)
https://leetcode.com/problems/132-pattern/ 下面是我的做法.后来又看了一个提示: https://discuss.leetcode.com/topic/678 ...
- [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 ...
- [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 bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- 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 ...
- 456 132 Pattern 132模式
给定一个整数序列:a1, a2, ..., an,一个132模式的子序列 ai, aj, ak 被定义为:当 i < j < k 时,ai < ak < aj.设计一个算法,当 ...
- LeetCode——456.132模式
给定一个整数序列:a1, a2, ..., an,一个132模式的子序列 ai, aj, ak 被定义为:当 i < j < k 时,ai < ak < aj.设计一个算法,当 ...
- 【LeetCode】456. 132 Pattern 解题报告(Python)
[LeetCode]456. 132 Pattern 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
随机推荐
- 【服务器数据恢复】RAID6数据恢复案例
服务器数据恢复环境:一台Web服务器中有一组由8块磁盘组建的raid6磁盘阵列,用来运行数据库和存储普通办公文件. 服务器故障:服务器raid6磁盘阵列中有两块硬盘离线,但是管理员没有注意到这种情况, ...
- 【运维】通过gotty实现网页代理访问服务器及K8S容器操作实践
Gotty 是Golang编写的可以方便的共享系统终端为web应用,是一个灵活强大的通过web访问终端的工具.本文将主要通过搭建Gotty实现对K8S容器的访问操作,开发如果想要正常的进行容器访问以及 ...
- #0延时与for循环+fork join_none
参考资料 (1) https://blog.csdn.net/moon9999/article/details/104207565 1.for循环+fork...join_none 注1: for循环 ...
- Rust字符串处理
Trim移除字符串开始末尾的字符串 fn main() { let s = " Hello Rust! "; // trim移除字符串开始末尾的空格 // "Hello ...
- centos7 python2.7.5安装mysqldb
1.安装pip,下载pip-20.3.1.tar.gz 解压python setup.py install 2.安装setuptools,下载setuptools-43.0.0.zip 解压p ...
- studiostyl.es网站scheme文件无法下载
原因网站引用Google的服务 解决方法:查看网页源码.选择选取页面中的元素,选择download按钮,如下图所示 downloadExisting后面的数字代表主题的ID. 在网站中输入https: ...
- java基于ssm框架开发的公交查询系统源码公交系统源码公交路线查询项目有论文
简介 java基于ssm的公交路线查询系统,用户可以查询公交站点公交车路线以及公交换乘方案,还可以查看公交车路线地图,以及该站点所有的公交车路线. 演示视频: https://www.ixigua.c ...
- 根据XML生成实体类
因为对接系统的XML所需映射的实体类有几十个,自己来处理不太现实,于是一直找寻找这样的工具,终于让我发现了jaxb2-maven-plugin: http://www.mojohaus.org/jax ...
- Word04 公司战略规划文档office真题
1.课程的讲解之前,先来对题目进行分析,首先需要在考生文件夹下,将Wrod素材.docx文件另存为Word.docx,后续操作均基于此文件,否则不得分. 2.这一步非常的简单,打开下载素材文件,在 ...
- VisualStudio2015使用C#.NET开发IOS程序时进行真机测试的设置参考
VS2015+Xamarin使用C#.NET开发跨平台APP已经完全可行了,对了WP和UWP程序,都是MS的,一定没有问题. 而对于ANDROID则是直接可以在VS中进行开发.部署到模拟器和真机,非常 ...