Optimal Division

You are given an integer array nums. The adjacent integers in nums will perform float division.

  • For example, for nums = [2,3,4], we will evaluate the expression "2/3/4".

However, you can add any number of parentheses at any position to change the priority of operations. You want to add these parentheses such that the value of the expression after evaluation is maximum.

Return the corresponding expression that has the maximum value in string format.

Note: your expression should not contain redundant parentheses.

Example 1
Inputnums = [1000,100,10,2]
Output"1000/(100/10/2)"
The maximum value is achieved by 1000/(100/10/2) = 200, and adding parentheses around 100/10 would be redundant because they do not change operation priority.
Example 2
Inputnums = [2,3,4]
Output"2/(3/4)"
2/(3/4) = 8/3 = 2.667, and no other parenthesization gives a greater value.

Constraints

  • 1 <= nums.length <= 10
  • 2 <= nums[i] <= 1000
  • There is only one optimal division for the given input.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate