-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy path2232-minimize-result-by-adding-parentheses-to-expression.js
More file actions
50 lines (46 loc) · 1.89 KB
/
2232-minimize-result-by-adding-parentheses-to-expression.js
File metadata and controls
50 lines (46 loc) · 1.89 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
/**
* 2232. Minimize Result by Adding Parentheses to Expression
* https://leetcode.com/problems/minimize-result-by-adding-parentheses-to-expression/
* Difficulty: Medium
*
* You are given a 0-indexed string expression of the form "<num1>+<num2>" where <num1>
* and <num2> represent positive integers.
*
* Add a pair of parentheses to expression such that after the addition of parentheses,
* expression is a valid mathematical expression and evaluates to the smallest possible
* value. The left parenthesis must be added to the left of '+' and the right parenthesis
* must be added to the right of '+'.
*
* Return expression after adding a pair of parentheses such that expression evaluates to the
* smallest possible value. If there are multiple answers that yield the same result, return
* any of them.
*
* The input has been generated such that the original value of expression, and the value of
* expression after adding any pair of parentheses that meets the requirements fits within
* a signed 32-bit integer.
*/
/**
* @param {string} expression
* @return {string}
*/
var minimizeResult = function(expression) {
const plusIndex = expression.indexOf('+');
const left = expression.slice(0, plusIndex);
const right = expression.slice(plusIndex + 1);
let minValue = Infinity;
let result = '';
for (let i = 0; i < left.length; i++) {
for (let j = 1; j <= right.length; j++) {
const leftPrefix = i === 0 ? 1 : parseInt(left.slice(0, i));
const leftNum = parseInt(left.slice(i), 10);
const rightNum = parseInt(right.slice(0, j), 10);
const rightSuffix = j === right.length ? 1 : parseInt(right.slice(j), 10);
const value = leftPrefix * (leftNum + rightNum) * rightSuffix;
if (value < minValue) {
minValue = value;
result = `${left.slice(0, i)}(${left.slice(i)}+${right.slice(0, j)})${right.slice(j)}`;
}
}
}
return result;
};