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. UI工具

    sketch figma Adobe Photoshop Adobe Illustrator adobe xd

  2. 使用 EMQX Cloud 桥接数据到 GCP Pub/Sub

    前不久,Google 宣布其旗下的 GCP IoT Core 即将在 2023 年 8 月 16 日停止提供服务.这意味着大量使用 GCP IoT Core 的用户可能需要将他们的 IoT 应用迁移到 ...

  3. 在Mac的哪里可以找到bashrc文件

  4. Centos安装后出现please make your choice from '1' to eter the license information spoke | 'q' to quit |'c' to continue |'r' to refresh

    这是要求用户阅读或者接收协议: 解决方法:输入"1",按Enter键   阅读许可协议,输入"2",按Enter键  接受许可协议,输入"q" ...

  5. centos7 添加自定义程序为系统服务

    centos6版本的系统服务是/etc/init.d启动脚本的方式,centos7采用强大的systemctl来管理系统服务,大幅提供了系统服务的运行效率,但是服务的配置和以前版本完全不同,这是很大的 ...

  6. localStorage/sessionStorage/cookie

    html5本地存储主要有两种localStorage和sessionStorage        都是使用相同api,存入后都是字符串类型 localStorage和sessionStorage使用方 ...

  7. redis info 对应参数详解

    https://blog.csdn.net/qq_27342265/article/details/123094422 info命令的使用方法有以下三种: info:部分Redis系统状态统计信息.i ...

  8. IDEA中已配置阿里镜像,但maven无法下载jar包的问题

    然后我还出现了一个问题,由于使用了HTTPS,存在着SSL证书验证的问题,因此我在IDEA中添加了一行配置: 配置如下: -Dmaven.wagon.http.ssl.insecure=true -D ...

  9. 模型 线框shader

    https://www.assetstore.unity3d.com/cn/?stay#!/content/21897

  10. Unity Random

    Random 不仅可以随机值,还可以随机其它属性,用了这么久,刚知道... 譬如: rotation  随机Rotation onUnitSphere  球体表面随机点 insideUnitCircl ...