-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevalRPN.js
91 lines (84 loc) · 3.34 KB
/
evalRPN.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
LEETCODE PROBLEM: Evaluate RPN; Medium Difficulty; Javascript
TIME COMPLEXITY : O(n), where n is reduced from the length of
the input arr * the length of the stack, ea
of which must be iterated once.
SPACE COMPLEXITY: O(n), where n = the length of the stack.
EXPLANATION:
Stacks are contiguous arrays of values. They are treated
differently than non-stack arrays in that the data stored
are accessed via principle of LIFO (last in first out).
The last index of a stack array is called the top of the
stack. It is the point at which data is added (pushed),
retrieved, modified, or removed (popped). Operations that
adhere to LIFO stacks require only O(1) constant time.
Though data in other locations can be accessed, doing so
is counter to the purpose of a stack as such operations
require O(n) time, the same as iterating any normal array.
In this problem, an input arr contains an expression written
in Reverse Polish Notation (RPN). RPN lists operands first
then operators. Eg:
rpn1 = [2, 1, +, 3, *]
rpn2 = [2, 1, 3, +, *]
To evaluate this, we apply the operators to the two preceding
operands.
rpn1 = [(2+1) * 3]
rpn2 = [(3+1) * 2]
As operations are carried out, the operands are popped from
the top of stack and the result is pushed on top.
rpn1 = [2, 1, +, 3, *] -> [3, 3, *] -> [9]
rpn2 = [2, 1, 3, +, *] -> [4, 2, *] -> [8]
Since the right number of operands and operators is guaranteed,
the stack should contain only one int after evaluation is done.
CONSTRAINTS:
1. Guaranteed that there will be no edge test-cases, that
a result is always possible and the result will always
fit a 32-bit integer allocation.
2. Guaranteed that all input arrays will contain only a
correct number, value, type of operands or operators.
3. Data in the input array are stored as String types so
operands must be converted to ints for evaluation.
4. Valid operators include +, -, *, / and test-cases will
only use these.
*/
/**
* Evaluate an RPN input arr and
* return the resulting value.
* @param {string[]} tokens
* @return {number}
*/
var evalRPN = function(tokens) {
// base case: input arr too small
if(tokens.length < 3) {
return 0;
}
// initialize a stack arr
let ops = new Array();
for(let i = 0; i < tokens.length; i++) {
let char = tokens[i];
if(char === "+") {
// convert char to int & pop operand
ops.push(ops.pop() + ops.pop());
}
else if(char === "-") {
let n1 = parseInt(ops.pop());
let n2 = parseInt(ops.pop());
ops.push(n2 - n1);
}
else if(char === "*") {
ops.push(ops.pop() * ops.pop());
}
else if(char === "/") {
let n1 = ops.pop();
let n2 = ops.pop();
ops.push(n2 / n1);
} else {
// char is an operand
ops.push(parseInt(char));
}
}
return ops[0];
};
// testing
let arr1 = ["2", "1", "+", "3", "*"];
console.log(evalRPN(arr1)); // expected output = 9