Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
rishi772001 authored Jun 22, 2020
1 parent e493ead commit 9a51a5e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Dynamic Programming/edit distance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# https://www.geeksforgeeks.org/edit-distance-dp-5/

s1 = "sunday"
s2 = "saturday"
n = len(s1)
m = len(s2)

arr = [[0 for i in range(m+1)] for i in range(n+1)]

for i in range(m+1):
arr[0][i] = i

for i in range(n+1):
arr[i][0] = i


for i in range(1,n+1):
for j in range(1,m+1):
if s1[i-1] == s2[j-1]:
arr[i][j] = arr[i-1][j-1]
else:

arr[i][j] = min(arr[i-1][j],arr[i][j-1],arr[i-1][j-1])+1


print(arr[n][m])
12 changes: 12 additions & 0 deletions Dynamic Programming/longest increasing subsequence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# https://www.geeksforgeeks.org/longest-increasing-subsequence-dp-3/

arr = [50, 3, 10, 7, 40, 80]
ans = []

for i in range(len(arr)):
c = 0
for j in range(i,len(arr)-1):
if(arr[j] < arr[j+1]):
c += 1
ans.append(c)
print(max(ans)+1)
31 changes: 31 additions & 0 deletions Dynamic Programming/min cost path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# https://www.geeksforgeeks.org/min-cost-path-dp-6/

cost= [ [1, 2, 3],
[4, 8, 2],
[1, 5, 3] ]
r = len(cost[0])
c = len(cost)

arr = [[0 for i in range(r)] for i in range(c)]

m = 2
n = 2

arr[0][0] = cost[0][0]

for i in range(1,m+1):
arr[i][0] = arr[i-1][0] + cost[i][0]

for i in range(1,n+1):
arr[0][i] = arr[0][i-1] + cost[0][i]


for i in range(1,m+1):
for j in range(1,n+1):
arr[i][j] = min(arr[i - 1][j - 1], arr[i - 1][j], arr[i][j - 1]) + cost[i][j]

for i in arr:
print(i)

print(arr[m][n])

0 comments on commit 9a51a5e

Please sign in to comment.