当前位置:网站首页>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]};
边栏推荐
猜你喜欢
What new materials are used in the large aircraft C919?
Go语学习笔记 - gorm使用 - 数据库配置、表新增 Web框架Gin(七)
bean的生命周期
物联网网关该怎么选
“AI教练”请进家,家庭智能健身蓬勃发展
专访蚂蚁:这群技术排头兵,如何做好底层开发这件事?| 卓越技术团队访谈录
selenium模块
什么是微服务?
MySQL master-slave replication configuration construction, one step in place
Graphical relational database design ideas, this is too vivid
随机推荐
sizeof
golang: Gorm配置Mysql多数据源
DP5340 domestic replacement for CM5340 stereo audio A/D converter chip
IDEA搜索插件无结果一直转圈圈的解决办法
c语言变量的存储方式和生存期 -考察
What new materials are used in the large aircraft C919?
C# 获取系统已安装的.NET版本
interface
理解和熟悉递归中的尝试
Oracle查看表空间使用率及爆满解决方案
How does Redis prevent oversold and inventory deduction operations?
sizeof
Go: go - redis based operation
MySQL off-topic [ORM thought analysis]
go : go gin返回JSON数据
From catching up to surpassing, domestic software shows its talents
Hex conversion...
从追赶到超越,国产软件大显身手
Interview with Ant: How do these technology pioneers do the bottom-level development well?| Excellent technical team interview
Electron使用romote报错 : Uncaught TypeError: Cannot read property ‘BrowserWindow‘ of undefined