-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimum-stack.js
More file actions
78 lines (67 loc) · 1.63 KB
/
Copy pathminimum-stack.js
File metadata and controls
78 lines (67 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class MinStack {
constructor() {
this.stack = [];
this.minStack = [];
}
/**
* @param {number} val
* @return {void}
*/
push(val) {
this.stack.push(val);
if (this.minStack.length === 0 || val <= this.minStack[this.minStack.length - 1]){
this.minStack.push(val);
} else{
this.minStack.push(this.minStack[this.minStack.length -1]);
}
}
/**
* @return {void}
*/
pop() {
this.stack.pop();
this.minStack.pop();
}
/**
* @return {number}
*/
top() {
return this.stack[this.stack.length - 1];
}
/**
* @return {number}
*/
getMin() {
return this.minStack[this.minStack.length - 1];
}
}
// PREP
// Design stack that does push, pop, top and get min/smallest number in stack
// Ex)
// Input: ["MinStack", "push", 1, "push", 2, "push", 0, "getMin", "pop", "top", "getMin"]
// Output: [null,null,null,null,0,null,2,1]
// Explanation:
// MinStack minStack = new MinStack();
// minStack.push(1);
// minStack.push(2);
// minStack.push(0);
// minStack.getMin(); // return 0
// minStack.pop();
// minStack.top(); // return 2
// minStack.getMin(); // return 1
//PSEUDO
// Need 2 stacks → main + minStack
// constructor()
// make main = []
// make minStack = []
// push(val)
// push val into main
// if minStack empty → push val
// else → push smaller of (val, top of minStack)
// pop()
// pop from main
// pop from minStack (keep same length)
// top()
// return top of main (last item)
// getMin()
// return top of minStack (smallest so far)