Skip to content

Problem 155 #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ As I work through this list I figure I would make a GitHub repo with my solution
35. [Implement Trie (Prefix Tree) #208](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/implement-trie-208.md)
36. [Coin Change #322](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/coin-change-322.md)
37. [Product of Array Except Self #238](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/product-of-array-238.md)
38. Min Stack #155
38. [Min Stack #155](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/min-stack-155.md)
39. Validate Binary Search Tree #98
40. Number of Islands #200
41. Rotting Oranges #994
Expand Down Expand Up @@ -129,6 +129,7 @@ In order to practice with similar data structures I'll be placing each problem i
- [Implement Queue using Stacks #232](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/implement-queue-stacks-232.md)
- [Maximum Depth of Binary Tree #104](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/depth-binary-tree-104.md)
- [Evaluate Reverse Polish Notation #150](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/polish-notation-150.md)
- [Min Stack #155](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/min-stack-155.md)

### Linked Lists

Expand Down
104 changes: 104 additions & 0 deletions medium/min-stack-155.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Min Stack

Page on leetcode: https://leetcode.com/problems/min-stack/

## Problem Statement

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

Implement the MinStack class:

- MinStack() initializes the stack object.
- void push(int val) pushes the element val onto the stack.
- void pop() removes the element on the top of the stack.
- int top() gets the top element of the stack.
- int getMin() retrieves the minimum element in the stack.

You must implement a solution with O(1) time complexity for each function.

### Constraints

- -2<sup>31</sup> <= val <= 2<sup>31</sup> - 1
- Methods pop, top and getMin operations will always be called on non-empty stacks.
- At most 3 \* 10<sup>4</sup> calls will be made to push, pop, top, and getMin

### Example

```
["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[0],[-3],[],[],[],[]]

Output
[null,null,null,null,-3,null,0,-2]

Explanation
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); // return -3
minStack.pop();
minStack.top(); // return 0
minStack.getMin(); // return -2
```

## Solution

- pop, push and peek (top) are constant with a normal stack
- minheap can read min val in constant time
- Do a stack with a linked list instead? How can I reassign min?

### Initial Pseudocode

1. Initialize array that will be a stack
2. Push: push to array, then sort?
3. Pop: pop off array, then sort?
4. Top: pop off return then push back on

### Optimized Solution

This solution use O(1) for all operations by utilizing two stacks. You can see an explanation of this approach here: https://www.youtube.com/watch?v=qkLl7nAwDPo

```javascript
const MinStack = function () {
this.stack = [];
this.minStack = [];
};

/**
* @param {number} val
* @return {void}
*/
MinStack.prototype.push = function (val) {
this.stack.push(val);
const minStackSize = this.minStack.length;
// update minStack with current min value
if (minStackSize === 0 || this.minStack[minStackSize - 1] > val) {
this.minStack.push(val);
} else {
this.minStack.push(this.minStack[minStackSize - 1]);
}
};

/**
* @return {void}
*/
MinStack.prototype.pop = function () {
this.stack.pop();
this.minStack.pop();
};

/**
* @return {number}
*/
MinStack.prototype.top = function () {
return this.stack[this.stack.length - 1];
};

/**
* @return {number}
*/
MinStack.prototype.getMin = function () {
return this.minStack[this.minStack.length - 1];
};
```