This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def countEditDistance(a, b): | |
w = len(a) | |
h = len(b) | |
t = [[1 for x in range(w + 1)] for y in range(h + 1)] | |
t[0][0] = 0 | |
for i in range(1, w + 1): | |
t[0][i] = t[0][i-1] + 1 | |
for i in range(1, h + 1): | |
t[i][0] = t[i-1][0] + 1 | |
for i in range(1, h + 1): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#count longest common subsequence | |
def countLCS(a, b, t): | |
m = len(a) | |
n = len(b) | |
for i in range(m + 1): | |
for j in range(n + 1): | |
if i == 0 or j == 0: | |
t[i][j] = 0 | |
elif a[i-1] == b[j-1]: | |
t[i][j] = t[i-1][j-1] + 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
def minCoins(c, m, n): | |
t = [(sys.maxsize - 1) for i in range(n+1)] | |
t[0] = 0 | |
l = [-1 for i in range(n+1)] | |
for i in range(m): | |
for j in range(1, n+1): | |
if j >= c[i]: | |
if t[j-c[i]] + 1 < t[j]: | |
t[j] = t[j-c[i]] + 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def minCoins(c, m, n): | |
t = [[1 for c in range(n+1)] for i in range(m+1)] | |
for i in range(1, m+1): | |
for j in range(1, n+1): | |
if j < c[i-1]: | |
t[i][j] = t[i-1][j] | |
else: | |
t[i][j] = t[i-1][j] + t[i][j-c[i-1]] | |
#print(c[i-1], j, j-c[i-1], t[i][j]) | |
#print(t[i]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
def minCostPath(a, m, n): | |
t = [[0 for c in range(n)] for r in range(m)] | |
t[0][0] = a[0][0] | |
for i in range(1, m): | |
t[i][0] = t[i-1][0] + a[i][0] | |
for i in range(1, n): | |
t[0][i] = t[0][i-1] + a[0][i] | |
for r in range(1, m): | |
for c in range(1, n): |