Tap here to switch tabs
Problem
Submissions

Problem: Minimize Result by Adding Parentheses to Expression

med
30 min
Try to solve the Minimize Result by Adding Parentheses to Expression problem.

Statement

You are given a string expression representing an addition expression of the form "<num1>+<num2>" where <num1> and <num2> represent positive integers.

Add exactly one pair of parentheses to expression such that the left parenthesis is placed anywhere to the left of '+' and the right parenthesis is placed anywhere to the right of '+'.

Evaluating the modified expression computes the product of the number before the left parenthesis, the sum inside the parentheses, and the number after the right parenthesis. If there is no number before the left parenthesis or after the right parenthesis, that factor is treated as 11.

Return the resulting string after adding parentheses that minimizes the evaluated value. If multiple valid expressions yield the minimum value, return any of them.

Constraints:

  • 3≤3 \leq expression.length ≤10\leq 10

  • expression consists of digits from ′1′'1' to ′9′'9' and ′+′'+'.

  • expression starts and ends with digits.

  • expression contains exactly one ′+′'+'.

  • The original value of expression and the value of expression after adding any valid pair of parentheses fits within a signed 3232 bit integer.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimize Result by Adding Parentheses to Expression

med
30 min
Try to solve the Minimize Result by Adding Parentheses to Expression problem.

Statement

You are given a string expression representing an addition expression of the form "<num1>+<num2>" where <num1> and <num2> represent positive integers.

Add exactly one pair of parentheses to expression such that the left parenthesis is placed anywhere to the left of '+' and the right parenthesis is placed anywhere to the right of '+'.

Evaluating the modified expression computes the product of the number before the left parenthesis, the sum inside the parentheses, and the number after the right parenthesis. If there is no number before the left parenthesis or after the right parenthesis, that factor is treated as 11.

Return the resulting string after adding parentheses that minimizes the evaluated value. If multiple valid expressions yield the minimum value, return any of them.

Constraints:

  • 3≤3 \leq expression.length ≤10\leq 10

  • expression consists of digits from ′1′'1' to ′9′'9' and ′+′'+'.

  • expression starts and ends with digits.

  • expression contains exactly one ′+′'+'.

  • The original value of expression and the value of expression after adding any valid pair of parentheses fits within a signed 3232 bit integer.