对于此类问题:

对于元素nums[i],找出往左/右走第一个比它小/大的数字

我们常常用递增栈/递减栈实现。

递增栈实现第一个比它小

递减栈实现第一个比它大

Example: 2  1  5  6  2  3

stack: 保证栈是递增的顺序,因此每个数进来之前先删除栈里比它大的数字。

    因此每个数,当它被pop出来时,它在栈里的前一个元素是左边第一个比它小的数,将它pop出来的数是右边第一个比它小的数。

(1) 2

(2) 1  (2被1pop出来,2左边第一个比它小的没有,右边第一个比它小的是1)

(3) 1  5

(4) 1  5  6

(5) 1  2    (5, 6 被 2 pop出来。对于5,左边第一个比它小的是1,右边第一个比它小的是2。对于6,左边第一个比它小的是5,右边第一个比它小的是2)

对于每个元素,因为只进栈出栈一次,所以总体的时间复杂度是O(n)

Increasing/ Decreasing Stack的更多相关文章

  1. [LeetCode] 1370. Increasing Decreasing String

    1. 原题链接:https://leetcode.com/problems/increasing-decreasing-string/ 2. 解题思路 直观的想法是:用有序map<char, i ...

  2. CF502C The Phone Number

    C. The Phone Number time limit per test 1 second memory limit per test 256 megabytes     Mrs. Smith ...

  3. 【Leetcode周赛】从contest-111开始。(一般是10个contest写一篇文章)

    Contest 111 (题号941-944)(2019年1月19日,补充题解,主要是943题) 链接:https://leetcode.com/contest/weekly-contest-111 ...

  4. Design and Analysis of Algorithms_Decrease-and-Conquer

    I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...

  5. MOOCULUS微积分-2: 数列与级数学习笔记 Review and Final

    此课程(MOOCULUS-2 "Sequences and Series")由Ohio State University于2014年在Coursera平台讲授. PDF格式教材下载 ...

  6. Monotonic Array LT896

    An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...

  7. Avoid RegionServer Hotspotting Despite Sequential Keys

    n HBase world, RegionServer hotspotting is a common problem.  We can describe this problem with a si ...

  8. CodeForces - 1017 C. The Phone Number(数学)

    Mrs. Smith is trying to contact her husband, John Smith, but she forgot the secret phone number! The ...

  9. cf 1017C

    C. The Phone Number time limit per test 1 second memory limit per test 256 megabytes input standard ...

随机推荐

  1. About Adultism and why things ar the way they are

    About - Adultism About Adultism and why things ar the way they are In this page we will try to clari ...

  2. JAVA并发实现二(线程中止)

    package com.subject01; public class InterruptDemo { public static void main(String[] args) { SimpleT ...

  3. startActivity与startActivityForResult的使用小结

    1.在一个Activity中想要启动进入另一个Activity时,通常我们使用startActivity这个方法来实现,代码如下: 其中MainActivity为源activity,ActivityL ...

  4. PHP设计模式笔记三:三种基本设计模式(工厂模式、单例模式、注册树模式) -- Rango韩老师 http://www.imooc.com/learn/236

    一.工厂设计模式 index.php $db = IMooc\Factory::createDatabase(); 使用工厂类的静态方法直接创建一个dababase对象,当类名发生修改时,在工厂里修改 ...

  5. python scrapy 基础

    scrapy是用python写的一个库,使用它可以方便的抓取网页. 主页地址http://scrapy.org/ 文档 http://doc.scrapy.org/en/latest/index.ht ...

  6. 短信猫编程的一些资料1(At指令发送短信)

    现在正在做TC35的项目, 下面分享一下这几天在网上找到的资料: 手机 SMS PDU 格式参考手册 1.相关的GSM   AT指令     与SMS有关的GSM   AT指令(from   GSM0 ...

  7. NSString 使用小结

    http://blog.csdn.net/ms2146/article/details/8654148

  8. OpenGL ES 2.0 变换

    基本变换都是通过将表示点坐标的向量与特定的变换矩阵相乘完成的. 进行基于矩阵的变换时,三位空间中点的位置需要表示成齐次坐标形式. 齐次坐标形式:在X.Y.Z3个坐标值后面增加第四个量W,未变换时W值一 ...

  9. hdu1054 树状dp

    B - 树形dp Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:10000KB     64bit ...

  10. hdu4296 贪心

    E - 贪心 Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768KB     64bit I ...