当前位置:网站首页>stack containing min function (js)
stack containing min function (js)
2022-07-30 08:10:00 【a small silver】
Define the data structure of the stack, please implement a min function in this type that can get the smallest element of the stack. In this stack, the time complexity of calling min, push and pop are all O(1).
Example:
MinStack minStack = new MinStack();minStack.push(-2);minStack.push(0);minStack.push(-3);minStack.min(); --> return -3.minStack.pop();minStack.top(); --> return 0.minStack.min(); --> return -2.Thinking Analysis:
In order to get the smallest element in the stack, it is easy to think of using a variable to store this element, but the question is, after this element goes out, what about the next smallest element?To solve this problem, you have to use an array to store the elements of the first, second, third, and so on. Define two arrays. The main array arr1 is used to pop and push the stack, and the secondary array arr2Used to store small elements.Here comes the point,
When pushing into the stack, if the element is less than or equal to the current minimum element, then the element should also be placed in arr2;
When popping the stack, if the element is the current smallest element, the element at the end of arr2 will also be popped out together;
As a result, the elements stored in arr2 get smaller toward the end, and the last element at the end of each read is the smallest element.
Code implementation:
var MinStack = function() {this.arr1 = []this.arr2 = []};/*** @param {number} x* @return {void}*/MinStack.prototype.push = function(x) {this.arr1.push(x)if (!this.arr2.length || (this.arr2.length && x <= this.arr2[this.arr2.length - 1])) {this.arr2.push(x)}};/*** @return {void}*/MinStack.prototype.pop = function() {const e = this.arr1.pop()if (this.arr2[this.arr2.length - 1] === e) {this.arr2.pop()}return e};/*** @return {number}*/MinStack.prototype.top = function() {return this.arr1[this.arr1.length - 1]};/*** @return {number}*/MinStack.prototype.min = function() {return this.arr2[this.arr2.length - 1]};边栏推荐
- 2020 ACM | MoFlow: An Invertible Flow Model for Generating Molecular Graphs
- golang: Gorm配置Mysql多数据源
- MySQL basics [naming convention]
- 使用navicat连接mysql数据库时常报的错误:2003、1698、1251
- export , export default,import完整用法
- Goto statements
- Keil编译大小和存储说明
- Mobile phone side scroll to page to specify location
- Go 使用mencached缓存
- taro 打包编译报错
猜你喜欢

Ali two sides: List several tips for Api interface optimization

MySql详解基础

Go语学习笔记 - gorm使用 - 数据库配置、表新增 Web框架Gin(七)

WinForm(一):开始一个WinForm程序

VR机器人教你如何正确打乒乓球

ETL为什么经常变成ELT甚至LET?

Ali Ermian: How many cluster solutions does Redis have?I answered 4

2020年度总结——品曾经,明得失,展未来

万能js时间日期格式转换

Map file analysis in Keil software
随机推荐
【COCI 2020/2021 Round #2 D】Magneti(DP)
golang : Zap log integration
When does MySQL use table locks and when does it use row locks?
Equation Derivation Proof of Vector Triple Product
2020年度总结——品曾经,明得失,展未来
node.js中实现对数据库的链式操作
Keil compile size and storage instructions
How does Redis prevent oversold and inventory deduction operations?
VR机器人教你如何正确打乒乓球
go : 使用 grom 删除数据库数据
MySQL题外篇【ORM思想解析】
2020 ACM | MoFlow: An Invertible Flow Model for Generating Molecular Graphs
分布式锁开发
2020 数学建模之旅
MySQL basics [naming convention]
Handler消息机制-Native层
Go combines Gin to export Mysql data to Excel table
Calculate the inverse source of the matrix (using the adjoint matrix, a 3x3 matrix)
一段神奇的没有主方法的代码
uniapp中canvas与v-if更“配”