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 ...
随机推荐
- vscode中使用powershell显示分支名
https://blog.csdn.net/weixin_43932597/article/details/125000557 windows powershell(或windows terminal ...
- linux中用命令导出、导入mysql数据库表
一.导出数据 1.使用场景:在没有数据库可视化工具的情况下备份导出数据库. 命令如下: mysqldump -u用户名 -p 数据库名 > 数据库名.sql mysqldump -u root ...
- 关闭win10“快速启动”功能
windows正常运行时间的统计问题 windows任务管理器中,在性能选项卡中可以查看系统的运行时间(开机时间) 正常情况下,如果电脑关机,那么再次开机后,该时间会被重置 但是,如果电脑处于上面所说 ...
- nginx,git,maven面试题
1.简述一下什么是Nginx,它有什么优势和功能? Nginx是一个web服务器和方向代理服务器,用于HTTP.HTTPS.SMTP.POP3和IMAP协议.因 它的稳定性.丰富的功能集.示例配置文件 ...
- CTF Show web入门 1——20(信息收集)wp和一些感想
web1 信息搜集 此题为 [从0开始学web]系列第一题 此系列题目从最基础开始,题目遵循循序渐进的原则 希望对学习CTF WEB的同学有所帮助. 开发注释未及时删除 此题有以上备注,可以想到备注未 ...
- system verilog与C语言的接口(包含使用方法以及实例)
资料来源 (1) sv绿皮书; (2) vcs user guide; (3) https://www.cnblogs.com/studyforever/p/5169243.html (4) syst ...
- Jmeter - Config Tips
Guideline: Jmeter.properties file should avoid to be modified , modified user.properties instead > ...
- php 图片加水印插件
问题:背景透明的水印图片,在加到原图上后不显示,待解决 <?php /** * 图片加水印(适用于png/jpg/gif格式) * * @author flynetcn * * @param $ ...
- R7-3 十六进制字符串转换成十进制非负整数
R7-3 十六进制字符串转换成十进制非负整数 分数 15 全屏浏览题目 切换布局 作者 颜晖 单位 浙大城市学院 输入一个以#结束的字符串,滤去所有的非十六进制字符(不分大小写),组成一个新的表示十六 ...
- /usr/bin/install: cannot create regular file `/usr/local/jpeg6/include/jconfig.h'
出现下列异常: /usr/bin/install -c -m 644 jconfig.h /usr/local/jpeg6/include/jconfig.h /usr/bin/install: ca ...