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 ...
随机推荐
- Codeforces Round #327 (Div. 1) D. Top Secret Task
D. Top Secret Task time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- CGI、FastCGI和php-fpm的区别
参考网上的描述,将网上的描述内容,整理进来: 首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者.web server(比如说nginx)只是 ...
- vbox下centos安装增加功能失败
一般都是:unable to find the sources of your current Linux kernel. 先尝试这个吧:yum install kernel kernel-heade ...
- 自己意淫的一个简陋的Python网站扫描器
使用的模块 threading.optparse.urllib2 本地需要放字典,名字需大写. 上代码 def request(url,pathName): try: import urllib2 p ...
- class 类(2)
类属性和实例属性 一个类实例化后,实例是一个对象,有属性.同样,类也是一个对象,它也有属性. >>> class A(object): ... x = 7 ... >>& ...
- c语言编程之sglib库的简单使用
说实话自从大学毕业后已经很久没有用c语言写过程序了,一般都是使用c++,c++的stl和boost等,这些代码库大大简化了我们的编程复杂度.由于最近某种原因在次开始用c写程序.我是个比较懒的人,比较喜 ...
- 写一个段落python代码推理list深浅
主要是针对嵌套列表问题. 列表套列表,究竟子列表那个更深... 这个问题想着就烦.假设嵌套10000万个列表是不是要统计10000个数再排序呢? 最后想了想用 list的extend功能 加上递归函数 ...
- Android-它们的定义Dialog
Android-它们的定义Dialog 2014年4月27日 星期天 天气晴朗 心情平静 本篇博文来分享一个也是开发中常常须要用到的功能-自己定义对话框,这里我用到了Android中的图形资源shap ...
- Android Fragment详解(五):Fragment与Activity通讯
与activity通讯 尽管fragment的实现是独立于activity的,可以被用于多个activity,但是每个activity所包含的是同一个fragment的不同的实例. Fragment可 ...
- RHEL-resolv.conf文件修改后重启被还原
修改resolve.conf文件之后,reboot或service restart network时,修改的内容被还原.关闭NetworkManager即可.# chkconfig |grep Net ...