Skip to content
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 @@ -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)_
- [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)_
69 changes: 69 additions & 0 deletions src/neetcode/stack/min-stack/solution.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
42 changes: 42 additions & 0 deletions src/neetcode/stack/min-stack/solution.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading