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)

时间: 2024-12-28 21:10:58

Increasing/ Decreasing Stack的相关文章

【故障解决】OGG-00446 错误解决

[故障解决]OGG-00446 Could not find archived log for sequence 一.1  BLOG文档结构图       一.2  前言部分   一.2.1  导读和注意事项 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不知道的知识,~O(∩_∩)O~: ① OGG-00446 Could not find archived log for sequence 的解决方法(重点) ② OGG基本的维护.排错操作     Tips:   

How Hystrix Works?--官方

https://github.com/Netflix/Hystrix/wiki/How-it-Works Contents Flow Chart Circuit Breaker Isolation Threads & Thread Pools Request Collapsing Request Caching Flow Chart The following diagram shows what happens when you make a request to a service depe

Fast-tracking approach for building routing topologies in fast-moving networks

In one embodiment, a local node in a communication network determines a set of its neighbor nodes, and determines a respective occurrence frequency at which each particular neighbor node is to be probed based on a rate of change in distance between t

MOOCULUS微积分-2: 数列与级数学习笔记 Review and Final

此课程(MOOCULUS-2 "Sequences and Series")由Ohio State University于2014年在Coursera平台讲授. PDF格式教材下载 Sequences and Series 本系列学习笔记PDF下载(Academia.edu) MOOCULUS-2 Solution Review Determine whether the series converges. 1. $\displaystyle\sum_{n=0}^{\infty}{n\

Poj 2796 单调栈

关于单调栈的性质,和单调队列基本相同,只不过单调栈只使用数组的尾部, 类似于栈. Accepted Code: 1 /************************************************************************* 2 > File Name: 2796.cpp 3 > Author: Stomach_ache 4 > Mail: [email protected] 5 > Created Time: 2014年07月21日 星期一

cf 1017C

C. The Phone Number time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Mrs. Smith is trying to contact her husband, John Smith, but she forgot the secret phone number! The only thing Mrs. Smit

C. The Phone Number

time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Mrs. Smith is trying to contact her husband, John Smith, but she forgot the secret phone number! The only thing Mrs. Smith remembered was tha

896. Monotonic Array - Easy

An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is monotone increasing if for all i <= j, A[i] <= A[j].  An array A is monotone decreasing if for all i <= j, A[i] >= A[j]. Return true if and only if

ARE 212 - Problem Set 2

ARE 212 - Problem Set 2Due March 10thPart I: Theory (For your practice only. Not required)1. Derive the sampling error of the GLS estimator by showing that bGLS ? 2. I used concentrated likelihood to derive the ML estimate for the CLRM. We could also