Algorithm


Problem Name: 553. Optimal Division

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.

 

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 <= 10
  • 2 <= nums[i] <= 1000
  • There is only one optimal division for the given input.

 
 

 

Code Examples

#1 Code Example with Java Programming

Code - Java Programming


class Solution {
    public String optimalDivision(int[] nums) {
        StringBuilder sb = new StringBuilder("");
        
        sb.append(nums[0]);
        
        for(int i=1;i < nums.length;i++) {
            if (i == 1 && nums.length > 2) {
                sb.append("/(").append(nums[i]);
            }
            else {
                sb.append("/").append(nums[i]);
            }
        }
        
        return nums.length > 2 ? sb.append(")").toString() : sb.toString();
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1000,100,10,2]

Output

x
+
cmd
"1000/(100/10/2)"

#2 Code Example with Javascript Programming

Code - Javascript Programming


onst optimalDivision = function(nums) {
  const len = nums.length
  if(len <= 2) return len === 2 ? `${nums[0]}/${nums[1]}` : nums.join('')
  let res = `${nums[0]}/(${nums[1]}`
  for(let i = 2; i  <  len; i++) {
    res += `/${nums[i]}`
  }
  return res + '>'
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1000,100,10,2]

Output

x
+
cmd
"1000/(100/10/2)"

#3 Code Example with Python Programming

Code - Python Programming


class Solution:
    def optimalDivision(self, nums):
        """
        :type nums: List[int]
        :rtype: str
        """
        if len(nums) == 1: return str(nums[0])
        elif len(nums) == 2: return str(nums[0])+"/"+str(nums[1])
        else: return str(nums[0])+"/("+"/".join(str(i) for i in nums[1:])+")"
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [2,3,4]

Output

x
+
cmd
"2/(3/4)"
Advertisements

Demonstration


Previous
#552 Leetcode Student Attendance Record II Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#554 Leetcode Brick Wall Solution in C, C++, Java, JavaScript, Python, C# Leetcode