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 ...
随机推荐
- 利用Visual Studio寻找C#程序必要的运行库文件
在工程打包中,有时候很头痛的就是运行所需要的库文件不能够全面的包含进来,特别是有时候调用了一系列外部扩展.对于这些问题,我们可以借用Visual Studio的打包功能帮助我们寻找软件运行必须的库文件 ...
- Linux内核设计基础(十)之内核开发与总结
(1)Linux层次结构: (2)Linux内核组成: 主要由进程调度(SCHED).内存管理(MM).虚拟文件系统(VFS).网络接口(NET)和进程间通信(IPC)等5个子系统组成. (3)与Un ...
- maven 打包源文件
1.The source plugin can be used to create a jar file of the project sources from the command line or ...
- 安装MySQL和Navicat,并与MyEclipse连接
1.下载安装MySQL 1)到http://dev.mysql.com/downloads/下载MySQL社区版Server,和用于JDBC的Connector.一路默认安装就可以,须要注意的是记住M ...
- 转载:android——eclipse如何去除Ctrl+shift+R组合键查找到的.class文件
转载自:http://blog.csdn.net/virgilli/article/details/22500409 AS里面的build文件下一堆的.class 文件,当你要定位资源文件的时候,有些 ...
- Android应用中使用Popupmenu
最终效果: 页面布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: ...
- Django初学笔记1.
1,安装python 和 Django , 参考网上教程(安装python,配置path, 安装django,配置path..) 2,查看django安装版本:cmd-->python--> ...
- 使用Flex 和 Red5开发简单视频直播功能
Flex 是一个高效.免费的开源框架,可用于构建具有表现力的 Web应用程序,这些应用程序利用Adobe Flash Player和Adobe AIR, 可以实现跨浏览器.桌面和操作系统.虽然只能使用 ...
- c++模板编程-异质链表
概念: 像一个普通的链表结点中,其中成员next通常是指向同类型结点的指针.这就约束了链表中结点必须是同一类型,从而整个链表都只能保存同一类型的数据.而异质链表则是让next指向任何一种类型,也包括存 ...
- Turbo Sort Add problem to Todo list Problem code: TSORT
def heap_sort(ary): n = len(ary) first = int(n / 2 - 1) for start in range(first, -1, -1): # 3~0 rev ...