diff --git a/README.md b/README.md index 9f1712e..4894398 100644 --- a/README.md +++ b/README.md @@ -93,4 +93,5 @@ - [Task #19](src/neetcode/sliding-window/minimum-window-substring) – _Minimum Window Substring_ - _[check the task](https://neetcode.io/problems/minimum-window-with-characters)_ - [Task #20](src/neetcode/sliding-window/sliding-window-maximum) – _Sliding Window Maximum_ - _[check the task](https://neetcode.io/problems/sliding-window-maximum)_ - Stack - - [Task #21](src/neetcode/stack/valid-parentheses) – _Valid Parentheses_ - _[check the task](https://leetcode.com/problems/valid-parentheses)_ \ No newline at end of file + - [Task #21](src/neetcode/stack/valid-parentheses) – _Valid Parentheses_ - _[check the task](https://leetcode.com/problems/valid-parentheses)_ + - [Task #22](src/neetcode/stack/min-stack) – _Min Stack_ - _[check the task](https://leetcode.com/problems/minimum-stack)_ \ No newline at end of file diff --git a/src/neetcode/stack/min-stack/solution.test.ts b/src/neetcode/stack/min-stack/solution.test.ts new file mode 100644 index 0000000..7b1eb3c --- /dev/null +++ b/src/neetcode/stack/min-stack/solution.test.ts @@ -0,0 +1,69 @@ +import { MinStack } from './solution'; + +describe('Min Stack | NeetCode | RoadMap | Testcases', () => { + test('#1 Keeps the minimum after pop', () => { + const stack = new MinStack(); + + stack.push(1); + stack.push(2); + stack.push(0); + + expect(stack.getMin()).toBe(0); + + stack.pop(); + + expect(stack.top()).toBe(2); + expect(stack.getMin()).toBe(1); + }); + + test('#2 Supports negative numbers', () => { + const stack = new MinStack(); + + stack.push(-2); + stack.push(0); + stack.push(-3); + + expect(stack.getMin()).toBe(-3); + + stack.pop(); + + expect(stack.top()).toBe(0); + expect(stack.getMin()).toBe(-2); + }); + + test('#3 Keeps duplicate minimum values', () => { + const stack = new MinStack(); + + stack.push(2); + stack.push(1); + stack.push(1); + stack.push(3); + + expect(stack.getMin()).toBe(1); + + stack.pop(); + stack.pop(); + + expect(stack.top()).toBe(1); + expect(stack.getMin()).toBe(1); + + stack.pop(); + + expect(stack.top()).toBe(2); + expect(stack.getMin()).toBe(2); + }); + + test('#4 Handles push after pop', () => { + const stack = new MinStack(); + + stack.push(10); + stack.pop(); + stack.push(20); + + expect(stack.top()).toBe(20); + + stack.push(-20); + + expect(stack.getMin()).toBe(-20); + }); +}); diff --git a/src/neetcode/stack/min-stack/solution.ts b/src/neetcode/stack/min-stack/solution.ts new file mode 100644 index 0000000..9188464 --- /dev/null +++ b/src/neetcode/stack/min-stack/solution.ts @@ -0,0 +1,42 @@ +export class MinStack { + curMin = Infinity; + minStack: number[] = []; + stack: number[] = []; + + /** + * Pushes the element `val` onto the stack. + * @param {number} val + * @return {void} + */ + push(val: number): void { + this.curMin = Math.min(this.curMin, val); + this.minStack.push(this.curMin); + this.stack.push(val); + } + + /** + * Removes the element on the top of the stack. + * @return {void} + */ + pop(): void { + this.stack.pop(); + this.minStack.pop(); + this.curMin = this.minStack[this.minStack.length - 1] ?? Infinity; + } + + /** + * gets the top element of the stack. + * @return {number} + */ + top(): number { + return this.stack[this.stack.length - 1]; + } + + /** + * retrieves the minimum element in the stack. + * @return {number} + */ + getMin(): number { + return this.curMin; + } +}