1
1
"""
2
2
solutions to problems in EPI
3
3
"""
4
+
5
+
4
6
#naive recursion
5
7
def cc (n , denoms ):
6
8
minCoins = 100
@@ -34,7 +36,7 @@ def greedyCC(n, denoms):
34
36
Find the number of ways to make change, given an amount change and
35
37
an array of denominations, coins.
36
38
http://www.geeksforgeeks.org/dynamic-programming-set-7-coin-change/
37
-
39
+ """
38
40
def numberWaysMakeChange (change , coins ):
39
41
# We need n+1 rows as the table is consturcted in bottom up
40
42
# manner using the base case 0 value case (n = 0)
@@ -57,14 +59,14 @@ def numberWaysMakeChange(change, coins):
57
59
memo [i ][j ] = x + y
58
60
59
61
return memo [- 1 ][- 1 ]
60
- print(numberWaysMakeChange(10, [1, 5, 10, 25]))
61
- """
62
+ # print(numberWaysMakeChange(10, [1, 5, 10, 25]))
63
+
62
64
"""
63
65
8/14/17
64
66
Giving an amount of change and a list of available denominations, coins, find the minimum
65
67
number of coins required to make c. makeChange is the optimized version of
66
68
makeChange1, my original solution
67
-
69
+ """
68
70
def makeChange (change , coins ):
69
71
70
72
memo = [0 ]+ [1000 ] * (change )
@@ -74,7 +76,7 @@ def makeChange(change, coins):
74
76
memo [amount ]= min (memo [amount - c ]+ 1 , memo [amount ])
75
77
return memo [- 1 ]
76
78
77
- print(makeChange(25, [1, 2, 5, 10]))
79
+ # print(makeChange(25, [1, 2, 5, 10]))
78
80
79
81
80
82
def makeChange1 (change , coins ):
@@ -86,13 +88,13 @@ def makeChange1(change, coins):
86
88
memo [amount ]= memo [amount - c ]+ 1
87
89
return memo [- 1 ]
88
90
89
- print(makeChange1(25, [1, 2, 5, 10]))
90
- """
91
+ # print(makeChange1(25, [1, 2, 5, 10]))
92
+
91
93
"""
92
94
8/4/17
93
95
Buy and sell one share of stock, given the array of prices. Cannot
94
96
short stock, (have to buy before selling). Return the max profit possible.
95
-
97
+ """
96
98
97
99
# Brute force, use two nested loops, find max from every possible transaction.
98
100
def maxProfit (prices ):
@@ -115,6 +117,6 @@ def maxProfit1(prices):
115
117
maxProfit = max (maxProfit , prices [i ]- lowestSoFar )
116
118
return maxProfit
117
119
118
- print(maxProfit1([20, 1, 9, 2, 10]))
119
- """
120
+ # print(maxProfit1([20, 1, 9, 2, 10]))
121
+
120
122
0 commit comments