对于此类问题:

对于元素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. Gwt ListBox选中自动触发事件

    以前用TreeView显示,需求更改 需要做一个ListBox控件显示数据,和HTML中的<Select>标签一样 编辑时候自动触发选中的数据子类: 1.只要自动触发了rootListBo ...

  2. web.xml配置DispatcherServlet

    1. org.springframework.web.servlet.DispatcherServlet 所在jar包: <dependency> <groupId>org.s ...

  3. (转)iOS7界面设计规范(2) - UI基础 - iOS应用解析

    今天再来一发,然后结束掉周六的忙碌,去吃零食,还有冰啤酒:其实现在打嗝还有小龙虾味儿呢. 第二篇更多的是从技术的角度对iOS界面组成原理进行了简单的解析,篇幅很短,可稍作了解:更多关于iOS开发入门的 ...

  4. python进阶之路之文件处理

    Python之文件处理 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !imp ...

  5. CSS基础知识之float

    前段时间写过一篇CSS基础知识之position,当时对float的理解不太准确,被慕课网多名读者指出(原文已修正,如有误导实在抱歉).现对float进行更深入的学习,在此把学习心得分享给大家. 浮动 ...

  6. Sublime Text3 Package Control和Emmet插件安装方法

    因为初学前端,所以今天安装了Sumblime Text 3,然后就停不下来去找Package Control的安装方法. 网络上我找到并尝试过的方法有两种,我使用的是用Python代码去安装并成安装成 ...

  7. 调试EF源码

    在解决方案中添加下载好的EF的源码的引用 在新建项目中添加程序集的引用 添加配置文件中的节点 测试 获取程序集的公钥标记: 使用sn.exe命令 使用reflector

  8. 《第一行代码》学习笔记29-内容提供器Content Provider(2)

    1.查询操作: if (cursor != null) { while (cusor.moveToNext()) { String column1 = cursor.getString(cursor. ...

  9. mysql查询分组归类函数-group_concat,通常与group_by一起使用

    select a.`name`,group_concat(b.name SEPARATOR'.') as persons from `group` as a,`person` as b,`person ...

  10. UI基础视图----UILabel总结

    UILabel是UIKit框架中非常常用的视图类,是UIView的子类,是UIWindow,UIImageView等的兄弟类,因为继承自UIView,所以继承了UIView中的属性和方法,大部分都可以 ...