-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path0716-max-stack.js
More file actions
92 lines (84 loc) · 2.19 KB
/
0716-max-stack.js
File metadata and controls
92 lines (84 loc) · 2.19 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* 716. Max Stack
* https://leetcode.com/problems/max-stack/
* Difficulty: Hard
*
* Design a max stack data structure that supports the stack operations and supports finding
* the stack's maximum element.
*
* Implement the MaxStack class:
* - MaxStack() Initializes the stack object.
* - void push(int x) Pushes element x onto the stack.
* - int pop() Removes the element on top of the stack and returns it.
* - int top() Gets the element on the top of the stack without removing it.
* - int peekMax() Retrieves the maximum element in the stack without removing it.
* - int popMax() Retrieves the maximum element in the stack and removes it. If there is more
* than one maximum element, only remove the top-most one.
*
* You must come up with a solution that supports O(1) for each top call and O(logn) for each
* other call.
*/
var MaxStack = function() {
this.stack = [];
this.maxHeap = new PriorityQueue((a, b) => a.val === b.val ? b.id - a.id : b.val - a.val);
this.nodeId = 0;
this.deleted = new Set();
};
/**
* @param {number} x
* @return {void}
*/
MaxStack.prototype.push = function(x) {
const id = this.nodeId++;
const node = { val: x, id };
this.stack.push(node);
this.maxHeap.enqueue(node);
};
/**
* @return {number}
*/
MaxStack.prototype.pop = function() {
this.cleanStack();
const node = this.stack.pop();
this.deleted.add(node.id);
return node.val;
};
/**
* @return {number}
*/
MaxStack.prototype.top = function() {
this.cleanStack();
return this.stack[this.stack.length - 1].val;
};
/**
* @return {number}
*/
MaxStack.prototype.peekMax = function() {
this.cleanMaxHeap();
return this.maxHeap.front().val;
};
/**
* @return {number}
*/
MaxStack.prototype.popMax = function() {
this.cleanMaxHeap();
const maxNode = this.maxHeap.dequeue();
this.deleted.add(maxNode.id);
return maxNode.val;
};
/**
* @return {void}
*/
MaxStack.prototype.cleanStack = function() {
while (this.stack.length && this.deleted.has(this.stack[this.stack.length - 1].id)) {
this.stack.pop();
}
};
/**
* @return {void}
*/
MaxStack.prototype.cleanMaxHeap = function() {
while (this.maxHeap.size() && this.deleted.has(this.maxHeap.front().id)) {
this.maxHeap.dequeue();
}
};