From bda8fb9740b4fba5d7f9b74d55fa047ebe70560d Mon Sep 17 00:00:00 2001 From: vikrantvikaasa27 Date: Fri, 17 Mar 2023 17:13:05 +0530 Subject: [PATCH 1/2] code Updated --- .../Python/Merge Elements/mergeElements.py | 62 ++++--------------- 1 file changed, 13 insertions(+), 49 deletions(-) diff --git a/dynamic_programing/Python/Merge Elements/mergeElements.py b/dynamic_programing/Python/Merge Elements/mergeElements.py index f5a6e536c2..b493b97227 100644 --- a/dynamic_programing/Python/Merge Elements/mergeElements.py +++ b/dynamic_programing/Python/Merge Elements/mergeElements.py @@ -1,54 +1,18 @@ -""" -Description +def mergeElements(arr): + n = len(arr) + dp = [[0 for _ in range(n)] for _ in range(n)] -You are given N elements, in an array A. You can take any 2 consecutive elements a and b and merge them. On merging you get a single element with value (a+b)%100 and this process costs you a*b. After merging you will place this element in place of those 2 elements. - - -If the sequence is [A1, A2, A3, A4] and you merge A2 and A3, you incur a cost of A2*A3 and the array becomes [A1, (A2+A3)%100, A4]. - - -Find the Minimum cost to merge all the elements into a single element. -""" - - -def mergeElements(l, r): - if l == r: - return 0 - - if dp[l][r] == -1: - total = 0 - for i in range(l, r + 1): - total += arr[i] - - summ = 0 - for mid in range(l, r): - summ += arr[mid] - ans = min( - ans, - mergeElements(l, mid) - + mergeElements(mid + 1, r) - + (summ) * (total - summ), - ) - - dp[l][r] = ans - return dp[l][r] + # Fill in the dp table diagonally + for gap in range(1, n): + for l in range(n - gap): + r = l + gap + total = sum(arr[l:r+1]) + dp[l][r] = float('inf') + for mid in range(l, r): + dp[l][r] = min(dp[l][r], dp[l][mid] + dp[mid+1][r] + total) + return dp[0][n-1] n = int(input()) arr = [int(x) for x in input().split()] -dp = [[-1 for i in range(4001)] for j in range(4001)] -print(mergeElements(0, n - 1)) - - -""" -The nice observation here is the value of the Final element remains fixed and is the sum of the range %100. - -So we can design a DP with the states -DP(l,r) = minimum cost to merge the segment into one element. - -Now range (l,r) will form one element from 2 elements in the final step. -So let's say the (l, mid) and (mid+1,r) range equivalents merge and produce the final element. - -So DP(l,r) = min(DP(l,mid) + DP(mid+1,r) + (sum(l,mid)%100)*(sum(mid+1,r)%100) ) for all mid in range [l,r). - -""" +print(mergeElements(arr)) From 811f8e6325cc6d9c388710bea91bf6e8220ee827 Mon Sep 17 00:00:00 2001 From: vikrantvikaasa27 Date: Fri, 17 Mar 2023 17:59:07 +0530 Subject: [PATCH 2/2] updated --- .../Python/Merge Elements/mergeElements.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/dynamic_programing/Python/Merge Elements/mergeElements.py b/dynamic_programing/Python/Merge Elements/mergeElements.py index b493b97227..0761dd6b3b 100644 --- a/dynamic_programing/Python/Merge Elements/mergeElements.py +++ b/dynamic_programing/Python/Merge Elements/mergeElements.py @@ -13,6 +13,12 @@ def mergeElements(arr): return dp[0][n-1] -n = int(input()) -arr = [int(x) for x in input().split()] +try: + arr = [int(x) for x in input().split()] +except: + print("enter a valid list") + arr = [int(x) for x in input().split()] + print(mergeElements(arr)) + + \ No newline at end of file