Skip to content

Commit b7d04e8

Browse files
committed
Add dynamic questions
1 parent b58ee74 commit b7d04e8

3 files changed

Lines changed: 49 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Concept is almost same as 01 Knapsack Problem
2+
3+
def min_coin(coins, total):
4+
cols = total + 1
5+
rows = len(coins)
6+
7+
t = [ [0] if col == 0 else float('inf') for col in range(cols)] for i in range(rows)]
8+
9+
for i in range(rows):
10+
for j in range(1, cols):
11+
if j < coins[i]:
12+
t[i][j] = t[i-1][j]
13+
else:
14+
t[i][j] = min(t[i-1][j], 1 + t[i][j-coins[i])
15+
16+
return t[rows-1][cols-1]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def lcs(s1, s2):
2+
cols = len(s1) + 1
3+
rows = len(s2) + 1
4+
5+
t = [[0 for i in range(cols)] for i in range(rows)]
6+
7+
max_length = 0
8+
9+
for i in range(1, rows):
10+
for j in range(1, cols):
11+
if s2[i-1] == s1[j-1]:
12+
t[i][j] = 1 + t[i-1][j-1]
13+
else:
14+
t[i][j] = max(t[i-1][j], t[i][j-1])
15+
16+
max_length = max(max_length, t[i][j])
17+
18+
return max_length
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def lcs(s1, s2):
2+
cols = len(s1) + 1
3+
rows = len(s2) + 1
4+
5+
t = [[0 for i in range(cols)] for i in range(rows)]
6+
7+
max_length = 0
8+
9+
for i in range(1, rows):
10+
for j in range(1, cols):
11+
if s[i-1] == s[j-1]:
12+
t[i][j] = t[i-1][j-1] + 1
13+
max_length = max(max_length, t[i][j])
14+
15+
return max_length

0 commit comments

Comments
 (0)