Skip to content

Commit 938824d

Browse files
committed
dp (min coins, ranking), graph(num shortest path)
1 parent 4d82686 commit 938824d

5 files changed

Lines changed: 200 additions & 18 deletions

File tree

TreeADT/check_full_binary_tree.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Node:
2+
def __init__(self, key):
3+
self.key = key
4+
self.left = None
5+
self.right = None
6+
7+
def isFullTree(root):
8+
if not root:
9+
return True
10+
if not root.left and not root.right:
11+
return True
12+
# if both subtree is not none
13+
if root.left and root.right:
14+
return isFullTree(root.left) and isFullTree(root.right)
15+
return False
16+
17+
if __name__ == '__main__':
18+
root = Node(10)
19+
root.left = Node(20)
20+
root.right = Node(30)
21+
22+
root.left.right = Node(40)
23+
root.left.left = Node(50)
24+
root.right.left = Node(60)
25+
root.right.right = Node(70)
26+
27+
root.left.left.left = Node(80)
28+
root.left.left.right = Node(90)
29+
root.left.right.left = Node(80)
30+
root.left.right.right = Node(90)
31+
root.right.left.left = Node(80)
32+
root.right.left.right = Node(90)
33+
root.right.right.left = Node(80)
34+
root.right.right.right = Node(90)
35+
36+
print "Binary tree is full: ", isFullTree(root)

dynamic_prog/minCoins.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Given an integer representing a given amount of change, write a
2+
# function to compute the total number of coins required to make
3+
# that amount of change. You can assume that there is always a
4+
# 1 coin
5+
6+
from datetime import datetime
7+
8+
9+
def minCoinsRec(change, coins):
10+
if change == 0:
11+
return 0
12+
tot_min_coins = 10000000
13+
for coin in coins:
14+
if change-coin >= 0:
15+
this_min_coins = minCoinsRec(change-coin, coins)
16+
if this_min_coins < tot_min_coins:
17+
tot_min_coins = this_min_coins
18+
return tot_min_coins + 1
19+
20+
21+
def minCoinsTopDownDP(change, coins, dp):
22+
if change == 0:
23+
return 0
24+
if dp[change] != -1:
25+
return dp[change]
26+
tot_min_coins = 99999999
27+
for coin in coins:
28+
if change-coin >= 0:
29+
this_min_coins = minCoinsTopDownDP(change - coin, coins, dp)
30+
if this_min_coins < tot_min_coins:
31+
tot_min_coins = this_min_coins
32+
dp[change] = tot_min_coins + 1
33+
return dp[change]
34+
35+
36+
def minCoinsBottomUpDP(change, coins, dp):
37+
dp[0] = 0
38+
for i in range(1, change+1):
39+
for coin in coins:
40+
if i-coin >= 0:
41+
curr_min_coins = dp[i-coin] + 1
42+
if curr_min_coins < dp[i]:
43+
dp[i] = curr_min_coins
44+
return dp[change]
45+
46+
47+
if __name__ == '__main__':
48+
coins = [1, 2, 5]
49+
change = 31
50+
s = datetime.now()
51+
print "minimum coins with recursion: ", minCoinsRec(change, coins)
52+
e = datetime.now()
53+
print "total time taken using recursion: ", e-s
54+
55+
dp = [-1] * (change+1)
56+
s = datetime.now()
57+
print "minimum coins with top down dp: ", minCoinsTopDownDP(change, coins, dp)
58+
e = datetime.now()
59+
print "total time taken using top down dp: ", e - s
60+
61+
dp = [99999999] * (change+1)
62+
s = datetime.now()
63+
print "minimum coins with bottom up dp: ", minCoinsBottomUpDP(change, coins, dp)
64+
e = datetime.now()
65+
print "total time taken using bottom up dp: ", e - s

dynamic_prog/ranking.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# MegaCorp wants to give bonuses to its employees based on how many lines of
2+
# codes they have written. They would like to give the smallest positive amount
3+
# to each worker consistent with the constraint that if a developer has written
4+
# more lines of code than their neighbor, they should receive more money.
5+
#
6+
# Given an array representing a line of seats of employees at MegaCorp,
7+
# determine how much each one should get paid.
8+
#
9+
# For example, given [10, 40, 200, 1000, 60, 30],
10+
# you should return [1, 2, 3, 4, 2, 1].
11+
12+
13+
def ranking(n, arr):
14+
out = [1] * n
15+
# pass 1 from left to right
16+
for i in range(1, n):
17+
if arr[i] > arr[i-1]:
18+
out[i] = out[i-1] + 1
19+
# pass 2 from right to left for correct ordering
20+
for i in range(n-2, -1, -1):
21+
if arr[i] > arr[i+1] and out[i] <= out[i+1]:
22+
out[i] = out[i+1] + 1
23+
# elif arr[i] == arr[i+1] and out[i] == out[i+1]:
24+
# out[i] = out[i + 1] + 1
25+
return out
26+
27+
28+
if __name__ == '__main__':
29+
n = 6
30+
inp = [10, 40, 200, 1000, 60, 30]
31+
out_arr = ranking(n, inp)
32+
print out_arr
33+
assert out_arr == [1, 2, 3, 4, 2, 1]
34+
35+
n = 10
36+
inp = [9, 2, 3, 6, 5, 4, 3, 2, 2, 2]
37+
print ranking(n, inp)

graph/numShortestPath.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from collections import defaultdict
2+
3+
4+
class Graph(object):
5+
def __init__(self, v):
6+
self.vertices = v
7+
self.graph = defaultdict(list)
8+
9+
def add_edge(self, u, v):
10+
self.graph[u].append(v)
11+
12+
def bfs(self, src, dist, paths):
13+
visited = [False] * self.vertices
14+
# distance of any node from itself is 0
15+
dist[src] = 0
16+
# there is always 1 way to move from node to itself
17+
paths[src] = 1
18+
19+
queue = list()
20+
queue.append(src)
21+
visited[src] = True
22+
23+
while queue:
24+
curr = queue.pop(0)
25+
for vertex in self.graph[curr]:
26+
if not visited[vertex]:
27+
queue.append(vertex)
28+
visited[vertex] = True
29+
30+
# print curr, vertex, dist[curr], dist[vertex], queue
31+
# new shortest path
32+
if dist[vertex] > dist[curr] + 1:
33+
dist[vertex] = dist[curr] + 1
34+
paths[vertex] = paths[curr]
35+
# another shortest path
36+
elif dist[vertex] == dist[curr] + 1:
37+
paths[vertex] += 1
38+
print dist
39+
print paths
40+
41+
def num_shortest_path(self, src, dst):
42+
dist = [1000000] * self.vertices
43+
paths = [0] * self.vertices
44+
g.bfs(src, dist, paths)
45+
return paths[dst]
46+
47+
48+
if __name__ == '__main__':
49+
g = Graph(7)
50+
g.add_edge(0, 1)
51+
g.add_edge(0, 2)
52+
g.add_edge(1, 2)
53+
g.add_edge(1, 3)
54+
g.add_edge(2, 3)
55+
g.add_edge(3, 4)
56+
g.add_edge(3, 5)
57+
g.add_edge(4, 6)
58+
g.add_edge(5, 6)
59+
60+
src = 2
61+
dst = 6
62+
print("Number of shortest path from %d to %d = %d" % (src, dst, g.num_shortest_path(src, dst)))

minCoinsRequired.py

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

0 commit comments

Comments
 (0)