-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathProblem43.js
65 lines (60 loc) · 1.69 KB
/
Problem43.js
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
// Problem 43
//
// This problem was asked by Amazon.
//
// Implement a stack that has the following methods:
//
// push(val), which pushes an element onto the stack
// pop(), which pops off and returns the topmost element of the stack.
// If there are no elements in the stack, then it should throw an error or return null.
// max(), which returns the maximum value in the stack currently.
// If there are no elements in the stack, then it should throw an error or return null.
// Each method should run in constant time.
//
// https://leetcode.com/problems/min-stack/description/
//
// O(1) Time complexity
// O(N) Space complexity
// N is the number of elements in the stack
class MaxStack {
/**
* Initializes a stack and maximum number stack. The maxNum stack represents the maximum number at each state
*/
constructor() {
this.stack = [];
this.maxNum = [];
}
/**
* Pushes an element onto the stack
* @param {number} val
*/
push(val) {
if (
this.maxNum.length === 0 ||
val >= this.maxNum[this.maxNum.length - 1]
) {
this.maxNum.push(val);
}
this.stack.push(val);
}
/**
* Pops off an element, and return the topmost element of the stack
* @return {number?}
*/
pop() {
if (this.stack.length === 0 && this.maxNum.length === 0) return null;
const val = this.stack.pop();
// check if we need to pop off the maxNum stack
if (val === this.maxNum[this.maxNum.length - 1]) this.maxNum.pop();
return val;
}
/**
* Returns the maximum value in the stack currently
* @return {number?}
*/
max() {
if (this.maxNum.length === 0) return null;
return this.maxNum[this.maxNum.length - 1];
}
}
export default MaxStack;