Title: #716 - Max Stack - LeetCode JavaScript Solutions
Open Graph Title: #716 - Max Stack - LeetCode JavaScript Solutions
X Title: #716 - Max Stack - LeetCode JavaScript Solutions
Description: Design a max stack data structure that supports the stack operations and supports finding the stack's maximum element. Implement the Ma...
Open Graph Description: Design a max stack data structure that supports the stack operations and supports finding the stack's maximum element. Implement the Ma...
X Description: Design a max stack data structure that supports the stack operations and supports finding the stack's maximum element. Implement the Ma...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/max-stack
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/max-stack","name":"#716 - Max Stack - LeetCode JavaScript Solutions","description":"Design a max stack data structure that supports the stack operations and supports finding the stack's maximum element. Implement the Ma...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"var MaxStack = function() {\n this.stack = [];\n this.maxHeap = new PriorityQueue((a, b) => a.val === b.val ? b.id - a.id : b.val - a.val);\n this.nodeId = 0;\n this.deleted = new Set();\n};\n\n/**\n* @param {number} x\n* @return {void}\n*/\nMaxStack.prototype.push = function(x) {\n const id = this.nodeId++;\n const node = { val: x, id };\n this.stack.push(node);\n this.maxHeap.enqueue(node);\n};\n\n/**\n* @return {number}\n*/\nMaxStack.prototype.pop = function() {\n this.cleanStack();\n const node = this.stack.pop();\n this.deleted.add(node.id);\n return node.val;\n};\n\n/**\n* @return {number}\n*/\nMaxStack.prototype.top = function() {\n this.cleanStack();\n return this.stack[this.stack.length - 1].val;\n};\n\n/**\n* @return {number}\n*/\nMaxStack.prototype.peekMax = function() {\n this.cleanMaxHeap();\n return this.maxHeap.front().val;\n};\n\n/**\n* @return {number}\n*/\nMaxStack.prototype.popMax = function() {\n this.cleanMaxHeap();\n const maxNode = this.maxHeap.dequeue();\n this.deleted.add(maxNode.id);\n return maxNode.val;\n};\n\n/**\n* @return {void}\n*/\nMaxStack.prototype.cleanStack = function() {\n while (this.stack.length && this.deleted.has(this.stack[this.stack.length - 1].id)) {\n this.stack.pop();\n }\n};\n\n/**\n* @return {void}\n*/\nMaxStack.prototype.cleanMaxHeap = function() {\n while (this.maxHeap.size() && this.deleted.has(this.maxHeap.front().id)) {\n this.maxHeap.dequeue();\n }\n};","keywords":"LeetCode 716, #716 - Max Stack, Hard, JavaScript solution","learningResourceType":"Code","isAccessibleForFree":true,"educationalLevel":"intermediate","interactivityType":"mixed"}
| author | LeetCodeJavascript.com |
| og:type | article |
| og:image | https://leetcodejavascript.com/og-image.jpg |
| og:site_name | LeetCode JavaScript Solutions |
| twitter:card | summary_large_image |
| twitter:url | https://leetcodejavascript.com/solutions/max-stack |
| twitter:image | https://leetcodejavascript.com/og-image.jpg |
| twitter:creator | @joshcrozier |
| theme-color | #1f2937 |
Links:
| LeetCodeJavascript.com | https://leetcodejavascript.com/ |
| Star on GitHub | https://github.com/JoshCrozier/leetcode-javascript |
| Back to all solutions | https://leetcodejavascript.com |
| View on LeetCode | https://leetcode.com/problems/max-stack/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/0716-max-stack.js |
| Stack | https://leetcodejavascript.com/tags/stack |
| Design | https://leetcodejavascript.com/tags/design |
| Linked List | https://leetcodejavascript.com/tags/linked-list |
| Ordered Set | https://leetcodejavascript.com/tags/ordered-set |
| Doubly-Linked List | https://leetcodejavascript.com/tags/doubly-linked-list |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width,initial-scale=1
Robots: index, follow