Minimize Result by Adding Parentheses to Expression LeetCode solution in JavaScript

1
/**
2
* @param {string} expression
3
* @return {string}
4
*/
5
var minimizeResult = function (expression) {
6
const [left, right] = expression.split("+");
7
let res = [`(${expression})`, +left + +right];
8

9
for (let i = 0; i < left.length; i++) {
10
const i1 = left.slice(0, i) || 1;
11
const i2 = left.slice(i);
12

13
for (let j = 1; j <= right.length; j++) {
14
const j1 = right.slice(0, j);
15
const j2 = right.slice(j) || 1;
16

17
const tempTotal = i1 * (+i2 + +j1) * j2;
18

19
if (res[1] > tempTotal) {
20
res[0] = `${left.slice(0, i)}(${i2}+${j1})${right.slice(j)}`;
21
res[1] = tempTotal;
22
}
23
}
24
}
25
return res[0];
26
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0