LeetCode 553, Medium. Topics: Array, Math, Dynamic Programming. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 19 parametrized pytest cases, and a playground notebook:
Problem
You are given an integer array nums. The adjacent integers in nums will perform the float division.
- For example, for
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.
Examples
Constraints
- 1 <= nums.length <= 10
- 2 <= nums[i] <= 1000
- There is only one optimal division for the given input.
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
Last modified on September 7, 2026