-
Notifications
You must be signed in to change notification settings - Fork 0
/
수식 최대화.js
62 lines (49 loc) · 1.37 KB
/
수식 최대화.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
/*
6가지 경우의 수 하나씩 해보면 됨
식의 길이도 짧으므로 앞에서부터 하나씩 찾아서 해보면 됨
*/
const priorities = ['*+-', '*-+', '+*-', '+-*', '-+*', '-*+'];
const parse = (expression) => {
const result = [];
let rest = expression;
while (rest.length > 0) {
const value = rest.match(/^[0-9]+/)[0];
result.push(Number(value));
if ('*-+'.includes(rest[value.length])) {
result.push(rest[value.length]);
}
rest = rest.slice(value.length + 1);
}
return result;
};
const calculate = (expression, operator) => {
const stack = [];
let i = 0;
const operationFn =
operator === '*'
? (one, another) => one * another
: operator === '+'
? (one, another) => one + another
: (one, another) => one - another;
while (i < expression.length) {
if (expression[i] === operator) {
stack.push(operationFn(stack.pop(), expression[i + 1]));
i += 2;
} else {
stack.push(expression[i]);
i += 1;
}
}
return stack;
};
function solution(expression) {
const parsed = parse(expression);
let sol = -Infinity;
priorities.forEach((priority) => {
const first = calculate(parsed, priority[0]);
const second = calculate(first, priority[1]);
const [result] = calculate(second, priority[2]);
sol = Math.max(sol, Math.abs(result));
});
return sol;
}