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. main(调用一个公共组件)

    app.vue <template> <div> <Student/> <School></School> </div> < ...

  2. 2021 icpc 沈阳 I 【分式线性变换的保交比性】

    分式线性变换的保交比性 对于分式线性变换,具有保交比性 应用 在复数域下,存在分式线性变换,给定三个输入和输出,再给定第四个输入,求其在这个分式线性变换下的输出. https://codeforces ...

  3. docker compose设置不同容器间通信

    docker compose新启动了一个容器,这个时候怎么去连接到其他容器呢,去容器里面ping发现不通.一般来说是因为和其他容器没有在一个网络环境里面.首先用命令查看一下当前存在哪些网络环境. 使用 ...

  4. 【git】7.5 git工具-搜索

    资料来源: (1) https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E6%90%9C%E7%B4%A2 1.git grep 注1:使用g ...

  5. uniapp 配置钉钉小程序package.json文件

    { "uni-app": { "scripts": { "mp-dingtalk": { "title": " ...

  6. mybatis-关联查询1-一对多关联查询

    或者多表单独查询方式

  7. centos6.5升级python3.6并安装boto3模块

    1.先升级openssl yum安装各种依赖,yum install gcc gcc-c++ autoconf automake zlib zlib-devel pcre-devel tar zxvf ...

  8. 吴恩达老师机器学习课程chapter05——评估模型

    吴恩达老师机器学习课程chapter05--评估模型 本文是非计算机专业新手的自学笔记,高手勿喷. 本文仅作速查备忘之用,对应吴恩达(AndrewNg)老师的机器学期课程第十章.第十一章. 目录 吴恩 ...

  9. 基于recorder.js H5录音功能

    兼容性 1.Chrome,FF,Edge,QQ,360(注:现有IE和Safari全版本不兼容) 2.其中Chrome47以上以及QQ浏览器强制要求HTTPS的支持 3.请尝试使用FF,Edge,36 ...

  10. shell转义字符

    \n      回车 \b      单词边界 \w      [a-Z0-9]中任意一个字符 \W      [^a-Z0-9] \d      [0-9]中任意一个数字 \D      [^0-9 ...