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 ...
随机推荐
- hdu2089:不要62(基础数位dp)
题意:规定一个合法的号码不能含有4或者是连续的62 给定区间[n,m] 问此区间内合法的号码的个数 分析:数位dp dp[i][j]代表 最高位为 j 的 i 位数有多少个合法的 然后按题目规则进行转 ...
- Quartz集成springMVC 的方案二(持久化任务、集群和分布式)
Quartz是一个开放源码项目,专注于任务调度器,提供了极为广泛的特性如持久化任务,集群和分布式任务等. Quartz核心是调度器,还采用多线程管理. 1.持久化任务:当应用程序停止运行时,所有调度信 ...
- hdu 5159 Card (期望)
Problem Description There are x cards on the desk, they are numbered from 1 to x. The score of the c ...
- Live555 实战之框架简单介绍
作者:咕唧咕唧liukun321 来自:http://blog.csdn.net/liukun321 上一篇文章简要介绍了怎样以共享库的方式交叉编译Live555,今天再来介绍live源代码框架. 先 ...
- AVR32开发环境搭建
下面是搭建AVR32开发环境的过程记录: 1.AVR32的编译环境下载 (到这里下载 as5installer-stable-5.1.208-full.exe) 如果你在安装的过程中碰到如下问题: ...
- UIImagePickerController从拍照、图库、相册获取图片
iOS 获取图片有三种方法: 1. 直接调用摄像头拍照 2. 从相册中选择 3. 从图库中选择 UIImagePickerController 是系统提供的用来获取图片和视频的接口: 用UIImage ...
- How to build UDK2015?
UDK2015 is a stable release of portions of the EDKII project. 本文记录在Win7下用VS2012 编译UDK2015的过程. Step1, ...
- 如何把一个表中的部分字段值插入到另一个表中去 这sql吊
Insert into JHAC_TB_CODE(CID,CODE,ADD_TIME,USERID,PRO_CODE,USERNAME) select f_code.FID,f_code.Fcod ...
- DTO学习系列之AutoMapper(六)----EntityFramework和AutoMapper的婚后生活
写在前面 我到底是什么? 越界的可怕 做好自己 后记 文章标题主要关键字:mapping DTOs to Entities,注意并不是“Entities to DTOs”,表示实体对象到DTO的转换, ...
- webform 验证控件
验证: 一.非空验证 RequiredFieldValidator ErrorMessage - 验证出错后的提示信息 ControlToValidate - 要验证的控件的ID Display - ...