553. Optimal Division LeetCode Solution
In this guide, you will get 553. Optimal Division LeetCode Solution with the best time and space complexity. The solution to Optimal Division problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Optimal Division solution in C++
- Optimal Division solution in Java
- Optimal Division solution in Python
- Additional Resources
Problem Statement of 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.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
553. Optimal Division LeetCode Solution in C++
class Solution {
public:
string optimalDivision(vector<int>& nums) {
string ans = to_string(nums[0]);
if (nums.size() == 1)
return ans;
if (nums.size() == 2)
return ans + "/" + to_string(nums[1]);
ans += "/(" + to_string(nums[1]);
for (int i = 2; i < nums.size(); ++i)
ans += "/" + to_string(nums[i]);
ans += ")";
return ans;
}
};
/* code provided by PROGIEZ */
553. Optimal Division LeetCode Solution in Java
class Solution {
public String optimalDivision(int[] nums) {
StringBuilder sb = new StringBuilder(String.valueOf(nums[0]));
if (nums.length == 1)
return sb.toString();
if (nums.length == 2)
return sb.append('/').append(nums[1]).toString();
sb.append("/(").append(nums[1]);
for (int i = 2; i < nums.length; ++i)
sb.append('/').append(nums[i]);
sb.append(')');
return sb.toString();
}
}
// code provided by PROGIEZ
553. Optimal Division LeetCode Solution in Python
class Solution:
def optimalDivision(self, nums: list[int]) -> str:
ans = str(nums[0])
if len(nums) == 1:
return ans
if len(nums) == 2:
return ans + '/' + str(nums[1])
ans += '/(' + str(nums[1])
for i in range(2, len(nums)):
ans += '/' + str(nums[i])
ans += ')'
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.