Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -94,4 +94,5 @@
- [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 #22](src/neetcode/stack/min-stack) – _Min Stack_ - _[check the task](https://leetcode.com/problems/minimum-stack)_
- [Task #22](src/neetcode/stack/min-stack) – _Min Stack_ - _[check the task](https://leetcode.com/problems/minimum-stack)_
Comment thread
TheGeniusOfEternity marked this conversation as resolved.
Outdated
- [Task #23](src/neetcode/stack/evaluate-reverse-polish-notation) – _Evaluate Reverse Polish Notation_ - _[check the task](https://neetcode.io/problems/evaluate-reverse-polish-notation)_
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { evalRPN } from './solution';

describe('Evaluate Reverse Polish Notation | NeetCode | RoadMap | Testcases', () => {
test('#1 Evaluates multiplication after addition', () => {
const tokens = ['2', '1', '+', '3', '*', '4', '-'];
const expected = 5;
const output = evalRPN(tokens);

expect(output).toBe(expected);
});

test('#2 Evaluates division and addition', () => {
const tokens = ['4', '13', '5', '/', '+'];
const expected = 6;
const output = evalRPN(tokens);

expect(output).toBe(expected);
});

test('#3 Evaluates a complex expression', () => {
const tokens = ['10', '6', '9', '3', '+', '-11', '*', '/', '*', '17', '+', '5', '+'];
const expected = 22;
const output = evalRPN(tokens);

expect(output).toBe(expected);
});

test('#4 Truncates division toward zero', () => {
const tokens = ['-7', '3', '/'];
const expected = -2;
const output = evalRPN(tokens);

expect(output).toBe(expected);
});
});
46 changes: 46 additions & 0 deletions src/neetcode/stack/evaluate-reverse-polish-notation/solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
type Operators = '+' | '-' | '*' | '/';
/**
* @param {string[]} tokens
* @return {number}
*/
export const evalRPN = (tokens: string[]): number => {
const stack: number[] = [];
const operators: Record<string, Operators | undefined> = {
'+': '+',
'-': '-',
'*': '*',
'/': '/',
};

for (const t of tokens) {
const op = operators[t];
if (op !== undefined) {
const b = stack.pop();
const a = stack.pop();
if (a !== undefined && b !== undefined) {
Comment thread
TheGeniusOfEternity marked this conversation as resolved.
switch (op) {
case '+': {
stack.push(a + b);
break;
}
case '-': {
stack.push(a - b);
break;
}
case '*': {
stack.push(a * b);
break;
}
case '/': {
stack.push(Math.trunc(a / b));
break;
}
}
}
} else {
stack.push(Number(t));
}
}

return stack.pop() ?? -1;
Comment thread
TheGeniusOfEternity marked this conversation as resolved.
};
Loading