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. 【服务器数据恢复】VSAN节点容量盘故障离线的数据恢复案例

    VSAN简介:VSAN是以vSphere内核为基础开发,可以扩展使用的分布式存储架构.该架构在vSphere集群主机中安硬盘及闪存构建VSAN存储层,通过存储进行管理与控制,最终形成一个共享存储层.V ...

  2. BIP设计器代码不生效的问题解决方案

    bip高级设计器里的前端代码:0租户问题解决在iuap_yonbuilder_service库里的hpa_extcode表hpa_extcode表里搜索billno字段,对应高级设计器里的单据编码拿到 ...

  3. c#笔记(四)——switch

    ---恢复内容开始--- using UnityEngine; using System.Collections;   public class Script1 : MonoBehaviour {   ...

  4. OpenCV Mat类数据存储方式

    参考BiliBili 于仕琪老师 avoid-memory-copy-in-opencv class CV_EXPORTS Mat { public: // some members int rows ...

  5. ssh 登陆 Host key verification failed.

    报错 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ WARNING: REMOTE HOST IDENTIFICATION ...

  6. 前端vue的JsPDF html2canvas 生成pdf并以文件流形式上传到后端(转载)

    原文地址 1.首先在文件内引入htmlToPdf.js这里代码引入了html2canvas和jspdf//需要 npm i html2Canvas 和 npm i jspdf 在这里将getPdf 这 ...

  7. C#中的ReferenceEquals、Equals以及==

    https://www.cnblogs.com/ArtofDesign/p/3615212.html   C#中有一共有四种相等性判断方法: //Object中定义的三个方法 public stati ...

  8. darkriscv笔记

    1 按照默认设置,前4K空间为ROM,后4K空间是RAM.为什么RAM需要初值,初值从哪儿来? 2 指令分类 LUI: AUTPC: JAL: JALR: BCC : BEQ/BGE /BGEU/BL ...

  9. No.1.3

    CSS层叠样式表   /* css注释 */ CSS引入方式 内嵌式:CSS写在style标签中 提示:style标签虽然可以写在页面任意位置,但是通常约定写在 head 标签中(作用范围:当前页面: ...

  10. 前端之JavaScript(Js)基础

    JavaScript,简称Js HTML三把利剑之一,浏览器可解析Js,看着和Java像,实则和Java并无关系,和Python.Go.C++等都是一门独立的语言. 一种轻量级的编程语言. 可插入 H ...