Last updated on February 4th, 2025 at 12:31 am
Here, we see an Optimal Division LeetCode Solution. This Leetcode problem is solved using different approaches in many programming languages, such as C++, Java, JavaScript, Python, etc.
List of all LeetCode Solution
Topics
Math, String
Companies
Amazon
Level of Question
Medium

Optimal Division LeetCode Solution
Table of Contents
1. Problem Statement
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
2. Coding Pattern Used in Solution
The coding pattern used in the provided code is the String Manipulation pattern. The problem involves constructing a specific string representation of a mathematical expression based on the input array. The logic revolves around iterating through the array, appending elements to a string (or StringBuilder
), and adding parentheses in specific cases.
3. Code Implementation in Different Languages
3.1 Optimal Division C++
class Solution { public: string optimalDivision(vector<int>& nums) { string s=""; int n=nums.size(); for(int i=0;i<n;i++){ if(i!=n-1){ s+=to_string(nums[i])+'/'; } else s+=to_string(nums[i]); } if(n<3){ return s; } int k=s.find("/"); s.insert(k+1,"("); s+=')'; return s; } };
3.2 Optimal Division Java
class Solution { public String optimalDivision(int[] nums) { if(nums.length==1){ return nums[0] + ""; }else if(nums.length==2){ StringBuilder sb=new StringBuilder(); sb.append(nums[0] + "/" + nums[1]); return sb.toString(); } StringBuilder sb=new StringBuilder(); sb.append(nums[0]); sb.append("/("); for(int i=1;i<nums.length-1;i++){ sb.append(nums[i] + "/"); } sb.append(nums[nums.length-1] + ")"); return sb.toString(); } }
3.3 Optimal Division JavaScript
var optimalDivision = function(nums) { const n = nums.length switch (n) { case 1: return nums[0] + '' case 2: return nums[0] + '/' + nums[1] default: { let s = nums[0] + '/(' for (let i = 1; i < n - 1; i++) { s += '' + nums[i] + '/' } s += nums[n - 1] + ')' return s } } };
3.4 Optimal Division Python
class Solution(object): def optimalDivision(self, nums): 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(num) for num in nums[1:]]) + ")"
4. Time and Space Complexity
Time Complexity | Space Complexity | |
C++ | O(n) | O(n) |
Java | O(n) | O(n) |
JavaScript | O(n) | O(n) |
Python | O(n) | O(n) |
- If the input array has one or two numbers, the code directly constructs the string representation of the division.
- For arrays with more than two numbers, the code wraps all numbers after the first one in parentheses to maximize the result of the division.
- The string is built using loops and concatenation (or
StringBuilder
in Java, orjoin
in Python).