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 s3
value 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 firstpop
out all numbers that are smaller than that number. The numbers that arepopped
out becomes candidate fors3
. - We keep track of the
maximum
of suchs3
(which is always the most recentlypopped
number from thestack
). - Once we encounter any number smaller than
s3
, we know we found a valid sequence sinces1 < s3
impliess1 < 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 ...
随机推荐
- UI工具
sketch figma Adobe Photoshop Adobe Illustrator adobe xd
- 使用 EMQX Cloud 桥接数据到 GCP Pub/Sub
前不久,Google 宣布其旗下的 GCP IoT Core 即将在 2023 年 8 月 16 日停止提供服务.这意味着大量使用 GCP IoT Core 的用户可能需要将他们的 IoT 应用迁移到 ...
- 在Mac的哪里可以找到bashrc文件
- Centos安装后出现please make your choice from '1' to eter the license information spoke | 'q' to quit |'c' to continue |'r' to refresh
这是要求用户阅读或者接收协议: 解决方法:输入"1",按Enter键 阅读许可协议,输入"2",按Enter键 接受许可协议,输入"q" ...
- centos7 添加自定义程序为系统服务
centos6版本的系统服务是/etc/init.d启动脚本的方式,centos7采用强大的systemctl来管理系统服务,大幅提供了系统服务的运行效率,但是服务的配置和以前版本完全不同,这是很大的 ...
- localStorage/sessionStorage/cookie
html5本地存储主要有两种localStorage和sessionStorage 都是使用相同api,存入后都是字符串类型 localStorage和sessionStorage使用方 ...
- redis info 对应参数详解
https://blog.csdn.net/qq_27342265/article/details/123094422 info命令的使用方法有以下三种: info:部分Redis系统状态统计信息.i ...
- IDEA中已配置阿里镜像,但maven无法下载jar包的问题
然后我还出现了一个问题,由于使用了HTTPS,存在着SSL证书验证的问题,因此我在IDEA中添加了一行配置: 配置如下: -Dmaven.wagon.http.ssl.insecure=true -D ...
- 模型 线框shader
https://www.assetstore.unity3d.com/cn/?stay#!/content/21897
- Unity Random
Random 不仅可以随机值,还可以随机其它属性,用了这么久,刚知道... 譬如: rotation 随机Rotation onUnitSphere 球体表面随机点 insideUnitCircl ...