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 ...
随机推荐
- python_test_0001_base_string
#!/usr/bin/python # -*- coding: UTF-8 -*- from lib_001_decorator_log_funcname import decorator_log_f ...
- 无界面Linux系统和Windows系统使用selenium爬取CNVD数据
因为CNVD官网采用了反爬机制,所以使用selenium能够更容易的爬取漏洞数据 1.在Windows中使用 注意根据chrome版本下载对应chromedriver 2.在无界面的Linux中使用 ...
- Panel容器中显示多个窗体并通过按钮实现窗体切换
Panel容器中显示多个窗体并通过按钮实现窗体切换 在项目开发中经常会有如下需求: 主窗体formMain中有个一Panle: 在Panel内显示多个窗体,如form1,form2--,分别通过不同按 ...
- PID模板
typedef struct{ float Kp,Ki,Kd; float Target; float Current; float Error[3]; float DeadZone; float O ...
- h5py学习(一)核心概念
因pandas的to_hdf5函数有bug TypeError: object of type 'int' has no len(),写dataframe数据出现了报错,遂决定直接使用h5py来写数据 ...
- 实例正常,page页损坏处理
1.select count(*) from XXXXX_homework; 可以查询 2.check table XXXXX_homework; 有报错,报连接失败. ERROR 2013 (HY0 ...
- 【Python】容器:列表(list)/字典(dict)/元组(tuple)/集合(set)
三.Python容器:列表(list)/字典(dict)/元组(tuple)/集合(set) 1.列表(list) 1.1 什么是列表 是一个'大容器',可以存储N多个元素简单来说就是其他语言中的数组 ...
- Dynamics CRM 安全模型的性能问题
性能问题对系统的影响可以是致命性的,一旦不重视,在不久的将来随时可能爆发,导致系统卡顿甚至无法操作,即时重启也无济于事:甚至极其难以发现.这里为自己记录一下过往的经验.系统一开始的设计,很大程度上决定 ...
- java中对list集合进行分组
java中对list集合进行分组 Map<String, List<HealthImport>> excelIdCardNull = importList.stream() . ...
- python sorted() 多重排序
前言: 最开始是因为看到一道题目, 用一行代码解决[1, 2, 3, 11, 2, 5, 3, 2, 5, 3] 输出[11, 1, 2, 3, 5] 本来想法很简单,先去重后排序 但是遇到一个难点 ...