You are given an integer array nums. The adjacent integers in nums will perform the float division.
nums = [2,3,4], we will evaluate the expression "2/3/4".However, you can add any number of parenthesis at any position to change the priority of operations. You want to add these parentheses such the value of the expression after the evaluation is maximum.
Return the corresponding expression that has the maximum value in string format.
Note: your expression should not contain redundant parenthesis.
Example 1:
Input: nums = [1000,100,10,2] Output: "1000/(100/10/2)" Explanation: 1000/(100/10/2) = 1000/((100/10)/2) = 200 However, the bold parenthesis in "1000/((100/10)/2)" are redundant since they do not influence the operation priority. So you should return "1000/(100/10/2)". Other cases: 1000/(100/10)/2 = 50 1000/(100/(10/2)) = 50 1000/100/10/2 = 0.5 1000/100/(10/2) = 2
Example 2:
Input: nums = [2,3,4] Output: "2/(3/4)" Explanation: (2/(3/4)) = 8/3 = 2.667 It can be shown that after trying all possibilities, we cannot get an expression with evaluation greater than 2.667
Constraints:
1 <= nums.length <= 102 <= nums[i] <= 1000Problem Overview: You get an array nums representing a chain of divisions: nums[0] / nums[1] / nums[2] / ... / nums[n-1]. You can insert parentheses anywhere to change evaluation order. The goal is to return a string expression that produces the maximum possible value.
Approach 1: Brute Force Parenthesization with Recursion/DP (Time: O(n^3), Space: O(n^2))
This problem resembles the classic matrix-chain multiplication structure. For every subarray [i, j], compute the minimum and maximum value obtainable by placing parentheses in different ways. Try every split point k where the expression becomes (i..k) / (k+1..j). Because division is not associative, both min and max values must be tracked for each interval. Dynamic programming reduces repeated work by storing results in a table. This guarantees correctness but requires evaluating many partitions and storing intermediate expressions, which makes it heavier than needed for this specific problem.
Approach 2: Greedy Observation (Time: O(n), Space: O(1))
The key insight comes from the behavior of division. For a / b / c / d, the result equals ((a / b) / c) / d by default. However, placing parentheses as a / (b / c / d) makes the denominator smaller because you divide b by multiple numbers. A smaller denominator increases the overall value. Therefore, to maximize the result, keep the first number outside and group all remaining numbers inside a single denominator.
If the array length is 1, return the number. If it is 2, simply return a/b. When the array has three or more elements, construct the expression nums[0] / (nums[1] / nums[2] / ... / nums[n-1]). This structure guarantees the largest value without exploring all parenthesis placements. Implementation is straightforward: iterate through the array and build the string while inserting one opening parenthesis after the first division and a closing parenthesis at the end.
This greedy rule works because every additional division inside the denominator shrinks it, which increases the overall quotient. The solution relies on simple math properties rather than heavy dynamic programming.
Recommended for interviews: Interviewers expect the greedy observation. Starting with the brute-force or DP reasoning shows you understand the search space of parenthesization. Recognizing that all numbers after the first should be grouped into a single denominator demonstrates the optimization insight and leads to the O(n) solution.
The key observation is that to maximize the result of the division, we should minimize the denominator. We achieve this by grouping as many elements as possible in the denominator so that the result becomes maximum. Hence, we should divide the first number by all the numbers after the first number treated as one single division.
The solution starts by checking if the length of the array is 1 or 2. For these cases, the return is straightforward as no extra parentheses are needed. For more than two numbers, the first number is divided by the expression of the rest, enclosed in parentheses to ensure that division occurs from left to right within the parentheses, thus maximizing the result.
Time Complexity: O(n), where n is the number of elements in nums. We iterate through the nums once to construct the string.
Space Complexity: O(n), for storing the joined string.
| Approach | Complexity |
|---|---|
| Greedy Approach | Time Complexity: O(n), where n is the number of elements in nums. We iterate through the nums once to construct the string. |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Parenthesization | Exponential | O(n) | Conceptual understanding of all possible parenthesis placements |
| Dynamic Programming (Interval DP) | O(n^3) | O(n^2) | When systematically computing min/max values for each subarray |
| Greedy Parenthesis Placement | O(n) | O(1) | Optimal production solution using the division property insight |
553. Optimal Division (Leetcode Medium) • Programming Live with Larry • 1,116 views views
Watch 7 more video solutions →Practice Optimal Division with our built-in code editor and test cases.
Practice on FleetCode