对于此类问题:

对于元素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. mybatis学习笔记第一讲

    第一步:先配置mybatis配置 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE confi ...

  2. Java IO 概述

    输入和输出-数据源和目标媒介 术语“输入”和“输出”有时候会有一点让人疑惑.一个应用程序的输入往往是另一个应用程序的输出.那么OutputStream流到底是一个输出到目的地的流呢,还是一个产生输出的 ...

  3. NSTimer 详细设置

    NSTimer 详细设置1:http://blog.csdn.net/davidsph/article/details/7899483 NSTimer 详细设置2:http://blog.csdn.n ...

  4. linux文件权限解说

    我们进入一个目录, 执行ls -l会显示该目录下所有档案(这里用"档案"这个词, 代表文件和文件夹)的详细信息. 如图: 一共有7列信息, 分别为: [权限], [连结数], [拥 ...

  5. [RxJS] Handling Multiple Streams with Merge

    You often need to handle multiple user interactions set to different streams. This lesson shows hows ...

  6. Android开发所有视频教程汇总

    1.Mars的Android开发视频教程作者讲解的很详细,很全面,系统.以前出了两套视频,分别是<Java4Android视频教程>.<Android视频教程>,以及最新刚新出 ...

  7. Docker中的一些命令

    可以交互的方式启动container $ sudo docker run -t -i ubuntu:14.04 /bin/bash 当这个Bash shell进程终止时,这个容器也停止了. docke ...

  8. 图片拉伸(有保护区域) resizableImageWithCapInsets

    在仿写QQ会话的时候背景蓝色图片是拉伸而来,但是有些地方是受保护的不能拉伸 所以定义了下面的工具类中的一个方法,专门拉伸图片 UIImageResizingModeStretch:拉伸模式,通过拉伸U ...

  9. 8 Hbase get方式获取数据

    package com.hikvision.hbase.vertify.test; import org.apache.hadoop.conf.Configuration; import org.ap ...

  10. hdu 1232畅通工程

    Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可以实现交通 ...