From 9a51a5e2af4e078fa8826b009b79b6a9578fe53c Mon Sep 17 00:00:00 2001 From: Rishi Raj <47425945+rishi772001@users.noreply.github.com> Date: Mon, 22 Jun 2020 19:34:03 +0530 Subject: [PATCH] Add files via upload --- Dynamic Programming/edit distance.py | 26 ++++++++++++++++ .../longest increasing subsequence.py | 12 +++++++ Dynamic Programming/min cost path.py | 31 +++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 Dynamic Programming/edit distance.py create mode 100644 Dynamic Programming/longest increasing subsequence.py create mode 100644 Dynamic Programming/min cost path.py diff --git a/Dynamic Programming/edit distance.py b/Dynamic Programming/edit distance.py new file mode 100644 index 0000000..1034b76 --- /dev/null +++ b/Dynamic Programming/edit distance.py @@ -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]) diff --git a/Dynamic Programming/longest increasing subsequence.py b/Dynamic Programming/longest increasing subsequence.py new file mode 100644 index 0000000..375ede3 --- /dev/null +++ b/Dynamic Programming/longest increasing subsequence.py @@ -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) diff --git a/Dynamic Programming/min cost path.py b/Dynamic Programming/min cost path.py new file mode 100644 index 0000000..fd5fba5 --- /dev/null +++ b/Dynamic Programming/min cost path.py @@ -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]) +