当前位置:网站首页>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]};边栏推荐
- go : delete database data using grom
- Ali: How many methods are there for multi-threaded sequential operation?
- selenium module
- 使用navicat连接mysql数据库时常报的错误:2003、1698、1251
- Hex conversion...
- What happens when @Bean and @Component are used on the same class?
- 获取controller中所有接口路径和名称
- 【MySQL】MySQL中如何实现分页操作
- taro 打包编译报错
- redis实现分布式锁的原理
猜你喜欢

When does MySQL use table locks and when does it use row locks?

万能js时间日期格式转换

What are the access modifiers, declaration modifiers, and keywords in C#?Literacy articles

2020 ACM | MoFlow: An Invertible Flow Model for Generating Molecular Graphs

Handler消息机制-Native层

Electron之初出茅庐——搭建环境并运行第一个程序

uniapp中canvas与v-if更“配”

RAID disk array

Graphical relational database design ideas, this is too vivid

ARM体系结构概述
随机推荐
SkiaSharp 之 WPF 自绘 拖曳小球(案例版)
Vue项目通过node连接MySQL数据库并实现增删改查操作
从 Google 离职,前Go 语言负责人跳槽小公司
Equation Derivation Proof of Vector Triple Product
Upload file -- file type, picture type, document type, video type, compressed package type
From catching up to surpassing, domestic software shows its talents
【COCI 2020/2021 Round #2 D】Magneti(DP)
MYSQL下载及安装完整教程
sizeof
What new materials are used in the large aircraft C919?
The first artificial intelligence safety competition officially launched
How to use Swagger, say goodbye to postman
this and super
Universal js time date format conversion
C# 获取系统已安装的.NET版本
taro 打包编译报错
上传文件--文件类型大全,图片类型,文档类型,视频类型,压缩包类型
Go uses freecache for caching
Go: use gorm query record
Architectural Design Guide How to Become an Architect