当前位置:网站首页>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]};
边栏推荐
猜你喜欢
selenium模块
从 Google 离职,前Go 语言负责人跳槽小公司
uniapp中canvas与v-if更“配”
从追赶到超越,国产软件大显身手
When does MySQL use table locks and when does it use row locks?
From catching up to surpassing, domestic software shows its talents
大飞机C919都用了哪些新材料?
Redis 如何实现防止超卖和库存扣减操作?
Playing script killing with AI: actually more involved than me
[GO Language Basics] 1. Why do I want to learn Golang and get started with GO language
随机推荐
assert
专访蚂蚁:这群技术排头兵,如何做好底层开发这件事?| 卓越技术团队访谈录
树状数组的基本用法
ARM体系结构概述
MySQL off-topic [ORM thought analysis]
ETL为什么经常变成ELT甚至LET?
雷总个人博客看到
[GO Language Basics] 1. Why do I want to learn Golang and get started with GO language
The introduction of AI meta-learning into neuroscience, the medical effect is expected to improve accurately
五号黯区靶场 mysql 注入之limit注入记录
golang: Gorm configures Mysql multiple data sources
千万级数据量的表,怎样最快速度查询?
大飞机C919都用了哪些新材料?
【COCI 2020/2021 Round #2 D】Magneti(DP)
Go: go - redis based operation
IDEA search plug-in has no results and the solution has been spinning in circles
ArrayList
Keil软件中map文件解析
C语言自定义类型详解
包含min函数的栈(js)