forked from AdaGold/stacks-queues-js
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathproblems.js
More file actions
62 lines (55 loc) · 1.88 KB
/
problems.js
File metadata and controls
62 lines (55 loc) · 1.88 KB
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
const Stack = require('./stack');
/*
Time Complexity: O(n), the function has to visit every node once
but operations performed in that loop are O(1)
Space Complexity: O(n), worst case scenario the stack could have on it
every character in the string
*/
const balanced = (str) => {
const stack = new Stack();
const pairs = { '{': '}', '[': ']', '(': ')' }
for (const char of str) {
if (pairs[char]) {
stack.push(char);
} else {
const top = stack.pop();
if (!(pairs[top] === char)) {
return false;
}
}
}
return stack.isEmpty();
}
/*
Time Complexity: O(n), the function has to check every character in expr,
but operations in the for loop are O(1)
Space Complexity: O(n), the controlling factor is the height of the stack,
which does not grow quickly with longer expr in a normal use case but I suppose
in a worst case scenario with some weird postfix expressions would increase in size
linear to the length of the weird expression you were using
*/
const evaluatePostfix = (expr) => {
const stack = new Stack();
for (const char of expr) {
const current = parseInt(char);
if (Number.isInteger(current)) {
stack.push(current);
} else {
const second = stack.pop();
const first = stack.pop();
if (char === '+') {
stack.push(first + second);
} else if (char === '-') {
stack.push(first - second);
} else if (char === '*') {
stack.push(first * second);
} else if (char === '/') {
const quotient = first / second;
quotient > 0 ? stack.push(Math.floor(quotient)) : stack.push(Math.ceil(quotient))
}
}
}
return stack.pop();
}
exports.balanced = balanced;
exports.evaluatePostfix = evaluatePostfix;