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 ...
随机推荐
- 网络-7 IPv6(下)
Ipv6实验 一.华为 思科与华为在接口启用ipv6 思科会自动产生local地址 华为不会自动产生local地址 以太网接口有mac地址,所以肯定是有eui64 serial接口是没有mac地址,他 ...
- csp-s2020 T2 动物园
题目简述: 共有n个动物,m条要求,每条要求描述了第x位上是1的话,必须购买y饲料,动物园里已有的动物必须的饲料已经购买了,问题是:在不要求增加购买饲料的基础上,还能放进去多少种动物?共有k个二进制, ...
- SqlServer 优化的技巧
1.避免使用 select * select * 不会走覆盖索引,会出现大量的回表操作,从而导致SQL的查询性能很低 2.用union all 代替 union 1.使用union后,可以获取排重复后 ...
- C# 抽象类小谈
最近在做一个测试系统,就是读取功率计上的数据,在上位机软件上显示出来. 电脑界面不是我们这次的主讲就略过,我们这里主要讲一个类的实现. 最开始领导给了一个 远方的三相功率计 给我让我写上位机,很简单, ...
- 记录自己在对订单进行按日期查询时使用的一种查询的方法,这里的orders是订单表,你也可以改成别的什么表对于最终数据不会造成影响,除非你那个表的数据只有几条那样就会出现查不到日期的情况
SELECT @date := DATE_ADD(@date, INTERVAL + 1 DAY) days FROM ( SELECT @date := DATE_ADD("2019-06 ...
- .Net Core WebApi + Vue前后端分离项目中的jwt令牌应用
1.Start up 里面配置JWT认证方案 //认证方案 services.AddAuthentication(option => { option.DefaultAuthenticateSc ...
- JAVA仓库管理系统(附源码+调试)
JAVA仓库管理系统--三只松鼠仓库管理系统功能描述(1)登录模块:登录信息等存储在数据库中(2)基本档案管理模块:供货商管理,销售商管理,货品档案管理,仓库管理(3)采购订货模块:用户可以通过查询条 ...
- 《SAP MDM主数据管理》.pdf
<SAP MDM主数据管理>.pdf 有需要的联系 wx :erpworld
- JS根据分数,计算名次(分数相同名次相同)
一开始的接口返回数据(数据已经拍好顺序,但是分数相同名次不同),如果数据未排序,先用sort排序,由大到小 分数相同名次相同,则需要以下操作即可 let prescore = 0;//初始分数 let ...
- QCheckBox CSS样式
QCheckBox:!hover { color:white; border-radius:10px; border:1px solid rgb(170, 170, 127); background- ...