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:

  1. Have a stack, each time we store a new number, we first pop out all numbers that are smaller than that number. The numbers that are popped out becomes candidate for s3.
  2. We keep track of the maximum of such s3 (which is always the most recently popped number from the stack).
  3. Once we encounter any number smaller than s3, we know we found a valid sequence since s1 < s3 implies s1 < s2.

132pattern-Leetcode456的更多相关文章

  1. 132-pattern(蛮难的)

    https://leetcode.com/problems/132-pattern/ 下面是我的做法.后来又看了一个提示: https://discuss.leetcode.com/topic/678 ...

  2. [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  ...

  3. [LeetCode] 132 Pattern 132模式

    Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that  ...

  4. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  5. 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 ...

  6. 456 132 Pattern 132模式

    给定一个整数序列:a1, a2, ..., an,一个132模式的子序列 ai, aj, ak 被定义为:当 i < j < k 时,ai < ak < aj.设计一个算法,当 ...

  7. LeetCode——456.132模式

    给定一个整数序列:a1, a2, ..., an,一个132模式的子序列 ai, aj, ak 被定义为:当 i < j < k 时,ai < ak < aj.设计一个算法,当 ...

  8. 【LeetCode】456. 132 Pattern 解题报告(Python)

    [LeetCode]456. 132 Pattern 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

随机推荐

  1. [转]idea 试用30天“无限续费”idea破解

    首先打开idea设置 左上角点击file-->setting-->Plugins   https://plugins.zhile.io 然后点击 确定 ("OK") 点 ...

  2. 在QAS里面,建立副本请求

  3. usb 2.0 request

  4. 20230225 TI Electromagnetic compatibility testing methods and standards

    Hello, and welcome to the TI Precision Labs video, Introducing Electromagnetic Compliance Standard T ...

  5. C#中DataTable新增列、删除列、更改列名、交换列位置

    一.新增列 1.1.新增列 /*新增列*/ dataTable.Columns.Add("列名称", Type.GetType("数据类型")); /*比如添加 ...

  6. qt 单元测试遇到的问题

    升级了qt creator到新版本.使用 google 单元测试,发现一个单元测试显示不对了. 测试结果输出:FATAL, 项目"t_ps"的测试未产生任何预期输出 比较了几个测试 ...

  7. go语言的结构体、指针、方法详解

    资源来自:https://blog.csdn.net/DXB2021/article/details/122652779 结体体定义如下: type author struct{ field1 typ ...

  8. centos 6.5 docker  安装

    https://www.cnblogs.com/zhangzhen894095789/p/6641981.html?utm_source=itdadao&utm_medium=referral

  9. Kubernetes学习笔记(三)

    [查看某个node上的pod] > kubectl get pods -o wide --namespace=kube-system | grep master-0         #node ...

  10. BottomNavigationBar 自定义 底部导航条、以及实现页面切换

    一.Flutter BottomNavigationBar 组件 BottomNavigationBar 是底部导航条,可以让我们定义底部 Tab 切换,bottomNavigationBar是 Sc ...