diff --git a/CoinChange.py b/CoinChange.py new file mode 100644 index 0000000..72e6a5f --- /dev/null +++ b/CoinChange.py @@ -0,0 +1,29 @@ +# Dynamic Programming Coin Change Python implementation of Coin +# Change problem +def count(S, m, n): + + # table[i] will be storing the number of solutions for + # value i. We need n+1 rows as the table is constructed + # in bottom up manner using the base case (n = 0) + # Initialize all table values as 0 + table = [0 for k in range(n+1)] + + # Base case (If given value is 0) + table[0] = 1 + + # Pick all coins one by one and update the table[] values + # after the index greater than or equal to the value of the + # picked coin + for i in range(0,m): + for j in range(S[i],n+1): + table[j] += table[j-S[i]] + + return table[n] + +# Driver program to test above function +arr = [1, 2, 3] +m = len(arr) +n = 4 +x = count(arr, m, n) +print (x) + diff --git a/RodCutting.py b/RodCutting.py new file mode 100644 index 0000000..a9bf84d --- /dev/null +++ b/RodCutting.py @@ -0,0 +1,24 @@ +# A Dynamic Programming solution for Rod cutting problem +INT_MIN = -32767 + +# Returns the best obtainable price for a rod of length n and +# price[] as prices of different pieces +def cutRod(price, n): + val = [0 for x in range(n+1)] + val[0] = 0 + + # Build the table val[] in bottom up manner and return + # the last entry from the table + for i in range(1, n+1): + max_val = INT_MIN + for j in range(i): + max_val = max(max_val, price[j] + val[i-j-1]) + val[i] = max_val + + return val[n] + +# Driver program to test above functions +arr = [1, 5, 8, 9, 10, 17, 17, 20] +size = len(arr) +print("Maximum Obtainable Value is " + str(cutRod(arr, size))) +