Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f0352ba

Browse files
Diego Flores CastilloDiego Flores Castillo
Diego Flores Castillo
authored and
Diego Flores Castillo
committed
minStack-007 (feature): implement minStack
1 parent aec3c7b commit f0352ba

File tree

2 files changed

+27
-31
lines changed

2 files changed

+27
-31
lines changed

LeetcodeProblems/Algorithms/Min_Stack.js

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,51 +19,45 @@ minStack.pop();
1919
minStack.top(); --> Returns 0.
2020
minStack.getMin(); --> Returns -2.
2121
*/
22-
2322
class MinStack {
2423
constructor() {
25-
this.minStack = [];
2624
this.stack = [];
27-
this.countStack = 0;
28-
this.countMinStack = 0;
2925
}
3026

3127
push(value) {
32-
if (this.countStack === this.stack.length) this.stack.push(value);
33-
else this.stack[this.countStack] = value;
34-
this.countStack++;
28+
const length = this.stack.length;
29+
this.stack[length] = value;
30+
}
31+
32+
getStack() {
33+
return this.stack;
34+
}
3535

36-
const min = this.getMin();
37-
if (min === null || min >= value) {
38-
if (this.countMinStack === this.minStack.length)
39-
this.minStack.push(value);
40-
else this.minStack[this.countMinStack] = value;
41-
this.countMinStack++;
36+
getMin() {
37+
let min = this.stack[0];
38+
for (let value of this.stack) {
39+
min = min > value ? value : min;
4240
}
41+
return min;
4342
}
4443

4544
pop() {
46-
if (this.countStack === 0) return null;
47-
48-
var elem = this.stack[this.countStack - 1];
49-
this.countStack--;
50-
51-
if (elem === this.minStack[this.countMinStack - 1]) this.countMinStack--;
52-
53-
return elem;
45+
const newStack = [];
46+
const length = this.stack.length;
47+
const number = this.stack[length - 1];
48+
for (let i = 0; i < length - 1; i++) {
49+
newStack[i] = this.stack[i];
50+
}
51+
this.stack = newStack;
52+
return number;
5453
}
5554

5655
top() {
57-
if (this.countStack === 0) return null;
58-
59-
return this.stack[this.countStack - 1];
56+
const length = this.stack.length;
57+
const number = this.stack[length - 1];
58+
return number;
6059
}
6160

62-
getMin() {
63-
if (this.countMinStack === 0) return null;
64-
65-
return this.minStack[this.countMinStack - 1];
66-
}
6761
}
6862

6963
module.exports.MinStack = MinStack;

LeetcodeProblemsTests/Algorithms/Min_Stack_Test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const assert = require('assert');
2-
const MinStack = require('../../LeetcodeProblems/Algorithms/Min_Stack').MinStack;
1+
const assert = require("assert");
2+
const MinStack = require("../../LeetcodeProblems/Algorithms/Min_Stack").MinStack;
33

44
function test() {
55
var minStack = new MinStack();
@@ -8,6 +8,8 @@ function test() {
88
minStack.push(-3);
99
assert.equal(minStack.getMin(), -3);
1010
assert.equal(minStack.pop(), -3);
11+
console.log(minStack.getStack());
12+
1113
assert.equal(minStack.top(), 0);
1214
assert.equal(minStack.getMin(), -2);
1315
}

0 commit comments

Comments
 (0)