Increasing/ Decreasing Stack
对于此类问题:
对于元素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的更多相关文章
- [LeetCode] 1370. Increasing Decreasing String
1. 原题链接:https://leetcode.com/problems/increasing-decreasing-string/ 2. 解题思路 直观的想法是:用有序map<char, i ...
- CF502C The Phone Number
C. The Phone Number time limit per test 1 second memory limit per test 256 megabytes Mrs. Smith ...
- 【Leetcode周赛】从contest-111开始。(一般是10个contest写一篇文章)
Contest 111 (题号941-944)(2019年1月19日,补充题解,主要是943题) 链接:https://leetcode.com/contest/weekly-contest-111 ...
- Design and Analysis of Algorithms_Decrease-and-Conquer
I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...
- MOOCULUS微积分-2: 数列与级数学习笔记 Review and Final
此课程(MOOCULUS-2 "Sequences and Series")由Ohio State University于2014年在Coursera平台讲授. PDF格式教材下载 ...
- Monotonic Array LT896
An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...
- Avoid RegionServer Hotspotting Despite Sequential Keys
n HBase world, RegionServer hotspotting is a common problem. We can describe this problem with a si ...
- CodeForces - 1017 C. The Phone Number(数学)
Mrs. Smith is trying to contact her husband, John Smith, but she forgot the secret phone number! The ...
- cf 1017C
C. The Phone Number time limit per test 1 second memory limit per test 256 megabytes input standard ...
随机推荐
- 一、mysql分表简单介绍
一.Mysql分表的原因 1.当一张的数据达到几百万时,你查询一次所花的时间会变多,如果有联合查询的话,我想有可能会死在那儿了. 分表的目的就在于此,减小数据库的负担,缩短查询时间. 2.mysql中 ...
- Java中线程池的学习
线程池的基本思想还是一种对象池的思想,开辟一块内存空间,里面存放了众多(未死亡)的线程,池中线程执行调度由池管理器来处理.当有线程任务时,从池中取一个,执行完成后线程对象归池,这样可以避免反复创建线程 ...
- JavaScript 克隆对象
function clone(origin) { return Object.assign({}, origin); } let aClone = { ...a }; // 等同于 let aClon ...
- React 入门最好的实例-TodoList
React 的核心思想是:封装组件,各个组件维护自己的状态和 UI,当状态变更,自动重新渲染整个组件. 最近前端界闹的沸沸扬扬的技术当属react了,加上项目需要等等原因,自己也决定花些时间来好好认识 ...
- Android ----------获取各种路径(更新中。。。。。。)
##在手机中的路径 *获取应用的路径,形式:/data/data/包名 String appDataDir = getApplicationInfo().dataDir; *获取手机数据存储路径,即/ ...
- jwplayer修改logo右键版权
jwplayer二次编译,可以自定义自己的logo和右键版权.
- Xcode快捷键 ---- 提高效率
Mac中主要有四个修饰键,分别是Command,Control,Option和Shift. 1. ⌘ + L 搜索行数,输入行数,调到指定行数 2.⌘ + shift + O 查询flie ...
- DTO学习系列之AutoMapper(二)
本篇目录: Flattening-复杂到简单 Projection-简单到复杂 Configuration Validation-配置验证 Lists and Array-集合和数组 Nested m ...
- C++中explicit
[explicit] 1.用于抑制隐式转换,即: X x = ; // error X x(); // ok 2.只对一个实参的构造函数有效,但是,可以用多有多个实参的构造函数,目前没有意义: cla ...
- 关于Repeater中绑定的控件不触发ItemCommand事件
今天遇到 在repeater 中使用一个button,点击button然后跳转另外一个页面. html. <asp:Repeater ID="repeater" runat= ...