Given a list of positive integers, the adjacent integers will perform the float division. For example, [2,3,4] -> 2 / 3 / 4.

However, you can add any number of parenthesis at any position to change the priority of operations. You should find out how to add parenthesis to get the maximum result, and return the corresponding expression in string format. Your expression should NOT contain redundant parenthesis.

Example:

Input: [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 don't 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



Note:

The length of the input array is [1, 10].
Elements in the given array will be in range [2, 1000].
There is only one optimal division for each test case.

Difficulty: interview

Starter code:

class Solution:
    def optimalDivision(self, nums: List[int]) -> str:
        

Solutions:
class Solution:
     def optimalDivision(self, nums):
         """
         :type nums: List[int]
         :rtype: str
         """
         if(len(nums) == 0):
             return ''
         if(len(nums) == 1):
             return str(nums[0])
         if(len(nums) == 2):
             return str(nums[0]) + '/' + str(nums[1])
         res = str(nums[0]) + '/' + '('
         for i in range(1,len(nums)-1):
             res += str(nums[i])
             res += '/'
         res += str(nums[-1])
         res += ')'
         return res

class Solution:
     def optimalDivision(self, nums):
         """
         :type nums: List[int]
         :rtype: str
         """
         s = '/'.join(map(str, nums))
         if len(nums) <= 2:
             return s
         s = s.replace('/', '/(', 1) + ')'
         return s


class Solution:
     def optimalDivision(self, nums):
         """
         :type nums: List[int]
         :rtype: str
         """
         
         
         if len(nums) == 1:
             return str(nums[0])
         if len(nums) == 2:
             return str(nums[0]) + '/' + str(nums[1])
         
         res = str(nums[0])+'/('
         
         for i in range(1,len(nums)-1,1):
             res = res + str(nums[i]) + '/'
         
         res = res + str(nums[len(nums)-1]) + ')'
         
         return res

class Solution:
     def optimalDivision(self, nums):
         """
         :type nums: List[int]
         :rtype: str
         """
         n = len(nums)
         if n==1:
             return str(nums[0])
         elif n==2:
             return str(nums[0])+'/'+str(nums[1])
         else:
             result = str(nums[0])+'/('
             for i in range(1,n-1):
                 result += str(nums[i])+'/'
             result += str(nums[-1])+')'
             return result


class Solution:
     def optimalDivision(self, nums):
         nums=list(map(str,nums))
         if len(nums)==1:
             return nums[0]
         if len(nums)==2:
             return "/".join(nums)
         nums[1]='('+nums[1]
         nums[-1]=nums[-1]+')'
         return "/".join(nums)

class Solution:
     def optimalDivision(self, nums):
         """
         :type nums: List[int]
         :rtype: str
         """
         res=''
         
         if len(nums)==1:
             return str(nums[0])
         
         if len(nums)==2:
             return str(nums[0])+'/'+str(nums[1])
         
         
         for i in range(0,len(nums)):
             if i==0:
                 res=res+str(nums[i])+'/('
             elif i<len(nums)-1:
                 res=res+str(nums[i])+'/'
             else:
                 res=res+str(nums[i])+')'
         return res

class Solution:
 
     def optimalDivision(self, nums):
         """
         :type nums: List[int]
         :rtype: str
         """
         table = [[None for i in range(len(nums))] for j in range(len(nums))]
         for i in range(len(nums)):
             table[i][i] = (nums[i], str(nums[i]))
         return self.findmax(table, 0, len(nums) - 1)[1]
 
     def findmax(self, table, i, j):
         if table[i][j] is not None:
             return table[i][j]
 
         if j - i == 1:
             a, a_s = table[i][i]
             b, b_s = table[j][j]
             ret = (a / b, a_s + '/' + b_s)
             table[i][j] = ret
             return ret
         m = -1
         ret = None
         for d in range(i, j):
             ma, max_rep = self.findmax(table, i, d)
             mi, min_rep = self.findmin(table, d+1, j)
             div = ma / mi
             if div > m:
                 m = div
                 ret = (max_rep, min_rep)
         rep = self.assemble(ret[0], ret[1])
         print("max %d, %d: v, %f, %s" % (i, j, m, rep))
         ret = (m, rep)
         table[i][j] = ret
         return ret
 
     def findmin(self, table, i, j):
         if table[j][i] is not None:
             return table[j][i]
 
         if j - i == 1:
             a, a_s = table[i][i]
             b, b_s = table[j][j]
             ret = (a / b, a_s + '/' + b_s)
             table[j][i] = ret
             return ret
 
         m = float('inf')
         ret = None
         for d in range(i, j):
             ma, max_rep = self.findmin(table, i, d)
             mi, min_rep = self.findmax(table, d+1, j)
             div = ma / mi
             if div < m:
                 m = div
                 ret = (max_rep, min_rep)
         rep = self.assemble(ret[0], ret[1])
         print("min %d, %d: v, %f, %s" % (i, j, m, rep))
         ret = (m, rep)
         table[j][i] = ret
         return ret
 
     def assemble(self, up, down):
         if '/' in down:
             return "%s/(%s)" % (up, down)
         else:
             return "%s/%s" % (up, down)

class Solution:
     def optimalDivision(self, nums):
         if (len(nums) == 1):
             return str(nums[0]) + ""
         elif (len(nums) == 2):
             return str(nums[0]) + "/" + str(nums[1])
         answer = str(nums[0]) + "/(" + str(nums[1])
         for i in nums[2:]:
             answer += "/" + str(i)
         answer += ")"
         return answer

class Solution:
     def optimalDivision(self, nums):
         """
         :type nums: List[int]
         :rtype: str
         """
         
         result = ""
         
         numsCount = len(nums)
         
         for i in range(numsCount):
             thisNum = nums[i]
             
             strNum = str(thisNum)
             
             if i == 0:
                 result = result + strNum
             elif i == 1 and numsCount > 2:
                 result = result + "/(" + strNum
             elif i == numsCount - 1 and numsCount > 2:
                 result = result + "/" +  strNum + ")"
             else:
                 result = result + "/" + strNum
         
         return result


class Solution:
     def optimalDivision(self, nums):
         """
         :type nums: List[int]
         :rtype: str
         """
         A = list(map(str, nums))
         if len(A) <= 2:
             return '/'.join(A)
         return A[0] + '/(' + '/'.join(A[1:]) + ')'

class Solution:
     def optimalDivision(self, nums):
         """
         :type nums: List[int]
         :rtype: str
         """
         if len(nums)<2:
             return str(nums[0])
         if len(nums)==2:
             return str(nums[0])+'/'+str(nums[1])
         l =str(nums[0])+'/('
         for x in nums[1:]:
             l = l+str(x)+'/'
         return l[:-1]+')'