알고리즘/LeetCode
[Leetcode][Javascript][Easy] 155. Min Stack
Benjamin_Choi
2021. 6. 30. 20:51
https://leetcode.com/problems/min-stack/
Min Stack - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
/**
* initialize your data structure here.
*/
var MinStack = function() {
this._value = [];
this._sortedValue = [];
};
/**
* @param {number} val
* @return {void}
*/
MinStack.prototype.push = function(val) {
this._value.push(val);
this._sortedValue.push(val);
this._sortedValue.sort((a, b) => a - b);
};
/**
* @return {void}
*/
MinStack.prototype.pop = function() {
const popValue = this._value.pop();
const idx = this._sortedValue.indexOf(popValue);
this._sortedValue.splice(idx, 1);
return popValue;
};
/**
* @return {number}
*/
MinStack.prototype.top = function() {
return this._value[this._value.length - 1];
};
/**
* @return {number}
*/
MinStack.prototype.getMin = function() {
return this._sortedValue[0];
};
속도가 느리다. 개선의 여지가 많다.