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];
};
속도가 느리다. 개선의 여지가 많다.
'알고리즘 > LeetCode' 카테고리의 다른 글
[Leetcode][Javascript][Medium] 7. Reverse Integer (0) | 2021.07.04 |
---|---|
[Leetcode][Javascript][Medium] 2. Add Two Numbers (0) | 2021.07.02 |
[Leetcode][Javascript][Easy] 1. Two Sum (0) | 2021.07.01 |
[Leetcode][Javascript][Easy] 20. Valid Parentheses (0) | 2021.06.28 |
[Leetcode][Javascript][Easy] 70. Climbing Stairs (0) | 2021.05.16 |
댓글