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 ...
随机推荐
- xfce-debian10 英文环境安装配置记录
Centos还没有用利索(因为我听说debian是更纯正的社区开源项目???可是这对于我这样毫无技术菜鸟来说有什么关系呢???可是耐不住心中的悸动???悸动又从哪里了呢???哎,不管了),突然心血来潮 ...
- 关于ecplipse中的中文都成乱码的问题
这个问题之前也搞死我了,差不多搞了两个下午才搞好 唉,说多了都是泪 时间过的有点久,不是很记得了,不过我这个问题是装fx包之前发生的,后来我是改了jdk版本的所以可能会有些不同 首先,中文会变成乱码主 ...
- IDEA EduTools Plugin Learning Cause
背景 编程培训需求,能够检测学生的输入内容与预期一致,有课程大纲 IDEA Plugin EduTools 是一个非常出色的培训工具,具备在IDE中学习,能够通过单元测试验证正确错误,能够设置用户输入 ...
- python学习:窗口程序
https://www.cnblogs.com/zyg123/p/10385456.html # 导入tkinter模块 import tkinter # 创建画布需要的库 from matplotl ...
- git将本地文件上传到远程仓库
要记住! 上传代码之前,一定要先下拉代码,如果有冲突(你和别人同时修改了某一个文件的某一行代码),那么就要先解决冲突,才能提交! 这里以将自己的本地文件上传至git仓库为例 1.首先进入需要上传的文件 ...
- Rinetd linxu TCP 端口转发
Rinetd是为在一个Unix和Linux操作系统中为重定向传输控制协议(TCP)连接的一个工具,实现端口映射/转发/重定向.Rinetd是单一过程的服务器,它处理任何数量的连接到在配置文件etc/r ...
- laravel常用集合的使用
创建集合: //数组1 $connection = collect([ ['name'=>'jack','age'=>'18','email'=>'ceshi@qq.com'], [ ...
- PowerShell学习笔记三_使用PS操作远程服务器
PowerShell远程操作服务器 参考: https://www.cnblogs.com/sparkdev/p/7200004.html 补充: 1. 服务器上,要被Powershell远程操作,是 ...
- Vulnhub 靶场 BEELZEBUB: 1
Vulnhub 靶场 BEELZEBUB: 1 前期准备 靶机地址:https://www.vulnhub.com/entry/beelzebub-1,742/ kali攻击机ip:192.168.1 ...
- 安装gitlab的总结
安装过程参考文档 http://www.linuxe.cn/post-520.html https://www.bilibili.com/read/cv4602641 备注要点: 1.gitlab安装 ...