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 ...
随机推荐
- class_schedule
#!/usr/bin/python # -*- coding: UTF-8 -*- class Schedule(object): def __init__(self, name=& ...
- 【36oj】 画圣诞树
原题 圣诞节要到了,不少商家在宣传板上绘制了圣诞树的图案,如图所示.一棵圣诞树由A和B两部分组成: A是由n(n≥)个呈三角形的字符矩阵构成的,每个字符矩阵由三个参数ai.bi.ci唯一确定.Ai表示 ...
- 【笔记】IDEA中maven导入依赖提示证书错误解决方法
先是提示:一定要备份配置文件!!! 一定要备份配置文件!!! 一定要备份配置文件!!! 先说原因:idea内置了jre,与你开发用的jre不是同一个软件,你通过命令修改的是开发用的jre的证书库,导入 ...
- 实验1task4
<实验结论> #include <stdio.h> #include <stdlib.h> int main() { int x, t, m; x = 123; p ...
- git命令,回滚上一个版本,回滚n个版本,撤销回滚
1 回滚到上一个版本 git checkout . :add之前的回滚 git reset --hard:add之后,commit之前 git reset --hard origin/test:com ...
- 学习高速PCB设计,这些走线方式你要知道! 高速射频百花潭 2022-01-21 08:53
1.电源布局布线相关 数字电路很多时候需要的电流是不连续的,所以对一些高速器件就会产生浪涌电流. 如果电源走线很长,则由于浪涌电流的存在进而会导致高频噪声,而此高频噪声会引入到其他信号中去. 而在高速 ...
- .Net的产品变迁
从开始接触.Net以来,转眼过了好几年.在这许多年里,不得不感慨.Net产品线也在不断发生着变化, 下面就简单的进行一下梳理. .Net能做桌面应用程序,也就是C/S,也能做网站开发,就是通常说的B/ ...
- Python 画极坐标图
需要用Python画极坐标等值线图,以下是所学的一些东西,特此记录 -------------------------------------------------- 翻译自 https://sta ...
- WPF_MVVM框架(5)
1.NuGet引用MVVM框架包 引入该框架包之后, 默认会在目录下创建ViewModel层的示例代码 2.第二步, 通过在MainViewModel中创建一些业务代码, 将其与MainWindow. ...
- cmake 设置属性INTERFACE_INCLUDE_DIRECTORIES,则其它库可以直接 target_link_libraries?
rs项目改为cpm下载 项目 leveldb 和 basiccache, basiccache依赖 leveldb,下载都是在主项目中, 设置 INTERFACE_INCLUDE_DIRECTORI ...