• [Leetcode] Divide and conquer, DP--241. Different Ways to Add Parentheses


    Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +- and *.

    Example 1

    Input: "2-1-1".

    ((2-1)-1) = 0
    (2-(1-1)) = 2

    Output: [0, 2]

    Example 2

    Input: "2*3-4*5"

    (2*(3-(4*5))) = -34
    ((2*3)-(4*5)) = -14
    ((2*(3-4))*5) = -10
    (2*((3-4)*5)) = -10
    (((2*3)-4)*5) = 10

    Output: [-34, -14, -10, -10, 10]

    Solution:

    1. 1st use divide and conquer   

            we can recursively divide the string into left and right part substring.

       then  recursively calculate the left and right part substring  result when there is only one numerical number and the other numberical number with a valid operator

       finally,  merge their results.

         note:
      how to divide?  iterate from the input string;  if there is a "+", "-", "*" operator,  we will divide the string into left and right substring.
      when there is only one numerical number, directly add into the result list
     


    1 def calcMath(num1, num2, operator): #python does not have switch/case 2 res = 0 3 if operator == "+": 4 res = num1 + num2 5 elif operator == "-": 6 res = num1 - num2 7 elif operator == "*": 8 res = num1 * num2 9 return res 10 11 result = [] 12 for i, c in enumerate(input): 13 if c == "+" or c == "-" or c == "*": 14 leftSubStr = input[0:i] 15 rightSubStr = input[i+1::] 16 17 leftLst = self.diffWaysToCompute(leftSubStr) 18 rightLst = self.diffWaysToCompute(rightSubStr) 19 #print ("ttttt ", i, c, leftLst, rightLst) 20 for ls in leftLst: 21 for rs in rightLst: 22 rs = calcMath(ls, rs, c) 23 result.append(rs) 24 #print ("ttttt ", result) 25 if len(result) == 0: 26 result.append(int(input)) 27 return result

       

      2.  2nd optimization 1st method; (Dynamic programming) using hashmap to store previous subproblem's result; Dynamic programming
      #in the previous calculations, there are repeated calculation of substring with same left substring and right substring calculation


    1 def calcMath(num1, num2, operator): #python does not have switch/case 2 res = 0 3 if operator == "+": 4 res = num1 + num2 5 elif operator == "-": 6 res = num1 - num2 7 elif operator == "*": 8 res = num1 * num2 9 return res 10 11 12 13 def diffWaysToComputeHelper(input, start, end): 14 15 result = [] 16 strKey = str(start) + "-" + str(end) 17 if strKey in dic: 18 result = dic[strKey] 19 return result 20 #print ("sssss ", result, start, end) 21 for i, c in enumerate(input[start:end+1]): 22 if c == "+" or c == "-" or c == "*": 23 leftLst = diffWaysToComputeHelper(input, start, start+i-1 ) 24 rightLst = diffWaysToComputeHelper(input, start+i+1, end) 25 #print ("xxxxx ", i, c, leftLst, rightLst, start, end) 26 for ls in leftLst: 27 for rs in rightLst: 28 rs = calcMath(ls, rs, c) 29 result.append(rs) 30 31 #print ("ttttt ", result, start, end, input[start:end+1]) 32 if len(result) == 0: 33 result.append(int(input[start:end+1])) 34 dic[strKey] = result 35 return result 36 37 dic = {} #memoization 38 return diffWaysToComputeHelper(input, 0, len(input)-1)

    --reference:

    http://www.geeksforgeeks.org/all-ways-to-add-parenthesis-for-evaluation/

  • 相关阅读:
    数论数论函数基础知识
    KMP入门和简单运用
    高斯消元入门
    FFT和NTT
    AC自动机入门和简单应用
    后缀自动机入门
    线段树优化建图的速成
    爱情九十七课,降低期待
    爱情九十二课,说出你的弱
    爱情八十六课,等得不是爱情
  • 原文地址:https://www.cnblogs.com/anxin6699/p/7057589.html
Copyright © 2020-2023  润新知