Skip to content

Commit 9a3a4c7

Browse files
committed
Added a few more DP problems
1 parent 22727af commit 9a3a4c7

File tree

4 files changed

+58
-29
lines changed

4 files changed

+58
-29
lines changed

dp/coinchange.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Problem: http://www.algorithmist.com/index.php/Coin_Change
3+
"""
4+
def coinchange(total, coins):
5+
M = len(coins)
6+
table = [[0]*M for i in range(total+1)]
7+
for i in range(M):
8+
table[0][i] = 1
9+
10+
for i in range(1, total+1):
11+
for j in range(M):
12+
# count of solutions excluding coin
13+
x = table[i][j-1] if j > 0 else 0
14+
15+
# count of solutions including coin
16+
y = table[i-coins[j]][j] if i - coins[j] >= 0 else 0
17+
table[i][j] = x + y
18+
19+
return table[total][M-1]
20+
21+
if __name__ == "__main__":
22+
print coinchange(10, [2, 3, 5, 6]) # 5
23+
print coinchange(5, [2, 3, 5]) # 2
24+
print coinchange(4, [1, 2, 3]) # 4

dp/kadane.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
The recurrence relation that we solve at each step is the following -
3+
4+
Let S[i] = be the max value contigous subsequence till the ith element
5+
of the array.
6+
7+
Then S[i] = max(A[i], A[i] + S[i - 1])
8+
At each step, we have two options
9+
1) We add the ith element to the sum till the i-1th elem
10+
2) We start a new array starting at i
11+
12+
We take a max of both these options and accordingly build up the array.
13+
"""
14+
def max_value_contigous_subsequence(arr):
15+
A = [arr[0]] + [0] * (len(arr) - 1)
16+
max_to_here = arr[0]
17+
for i in range(1, len(arr)):
18+
A[i] = max(arr[i], arr[i] + A[i-1])
19+
max_to_here = max(max_to_here, A[i])
20+
return max_to_here
21+
22+
if __name__ == "__main__":
23+
x = [-2, -3, 4, -1, -2, 1, 5, -3]
24+
y = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
25+
z = [-1, 3, -5, 4, 6, -1, 2, -7, 13, -3]
26+
print map(max_value_contigous_subsequence, [x, y, z])

misc/kadane.py

Lines changed: 0 additions & 20 deletions
This file was deleted.

trees/binarysearchtree.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@ def __init__(self, value):
1212
def __repr__(self):
1313
return "Node with value - %s" % self.value
1414

15-
1615
class BinarySearchTree(object):
1716
def __init__(self):
1817
self.root = None
1918
self.len = 0
2019

2120
def __len__(self):
22-
return self.len
21+
return self.len
2322

2423
def is_empty(self):
2524
return self.root == None
@@ -41,7 +40,7 @@ def _postorder(self, node, values):
4140
self._postorder(node.left, values)
4241
self._postorder(node.right, values)
4342
values.append(node.value)
44-
43+
4544
def values(self, reverse = False, order="in"):
4645
values = []
4746
if order == "in":
@@ -55,7 +54,7 @@ def values(self, reverse = False, order="in"):
5554
return values
5655

5756
def _search(self, root, value):
58-
if not root or root.value == value:
57+
if not root or root.value == value:
5958
return root
6059
if value < root.value:
6160
return self._search(root.left, value)
@@ -80,11 +79,11 @@ def get_min(self):
8079
def get_max(self):
8180
""" returns the element with the maximum value """
8281
return self._extremes(self.root, find_min=False)
83-
82+
8483
def successor(self, value):
8584
""" returns the successor of the element with value - value"""
8685
node = self.find_element(value)
87-
if not node:
86+
if not node:
8887
return None
8988
if node.right:
9089
return self._extremes(node.right, find_min=True)
@@ -115,14 +114,14 @@ def insert(self, value):
115114
new_node.parent = parent
116115
self.len += 1
117116
return
118-
117+
119118
def delete(self, value):
120119
""" deletes a node from tree with value - value """
121120
node = self.find_element(value)
122121
if not node:
123122
return None
124123
if not node.left or not node.right:
125-
node_spliced = node
124+
node_spliced = node
126125
else:
127126
node_spliced = self.successor(node.value)
128127
if node_spliced.left:
@@ -137,7 +136,7 @@ def delete(self, value):
137136
node_spliced.parent.left = temp_node
138137
else:
139138
node_spliced.parent.right = temp_node
140-
139+
141140
if node != node_spliced:
142141
node.value = node_spliced.value
143142
return node_spliced

0 commit comments

Comments
 (0)