Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added some DSA problems related to dynamic programming #21

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Dynamic Programming/EditDistance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Given : 2 Strings --> word1, word2
Return : Minimum no. of operations required to convert word1 to word2
Permitted operations : * Insert a character
* Delete a character
* Replace a character
*/

public class EditDistance {
public static void EditDistance(String str1, String str2) {
int n = str1.length();
int m = str2.length();

int[][] dp = new int[n+1][m+1];

// initialization
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp[0].length; j++) {
if(i == 0 || j == 0) {
dp[i][0] = i;
dp[0][j] = j;
}
}
}

for (int i = 1; i < dp.length; i++) {
for (int j = 1; j < dp[0].length; j++) {
if(str1.charAt(i-1) == str2.charAt(j-1)) {
dp[i][j] = dp[i-1][j-1];
} else {
dp[i][j] = Math.min(dp[i][j-1], Math.min(dp[i-1][j], dp[i-1][j-1])) + 1;
}
}
}
System.out.println(dp[n][m]);
}
public static void main(String[] args) {
String str1 = "intention";
String str2 = "execution";
EditDistance(str1, str2);
}
}
39 changes: 39 additions & 0 deletions Dynamic Programming/LongestCommonSubsequenceCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Longest Common Subsequence problem using Tabulation.

A subsequence of a string is a new string generated from the original string with some characters (can be none) dleted without changing the relative order of the remaining characters.
*/

import java.util.*;

public class LongestCommonSubsequenceCode {

public static void main(String[] args) {
String str1 = "abcde";
String str2 = "ace";
int[][] val = new int[str1.length() + 1][str2.length() + 1];
for (int[] row : val) {
for (int i = 0; i < row.length; i++) {
row[i] = -1;
}
}

for (int i = 0; i < val.length; i++) {
for (int j = 0; j < val[0].length; j++) {
if (i == 0 || j == 0) {
val[i][j] = 0;
}
}
}
for (int i = 1; i < val.length; i++) {
for (int j = 1; j < val[0].length; j++) {
if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
val[i][j] = val[i - 1][j - 1] + 1;
} else {
val[i][j] = Math.max(val[i - 1][j], val[i][j - 1]);
}
}
}
System.out.println(val[str1.length()][str2.length()]);
}
}
31 changes: 31 additions & 0 deletions Dynamic Programming/RodCuttingCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Rod Cutting Problem using Tabulation Method.

Given : Rod --> 'n' inches (length)
Array of prices for all pieces of size smaller than n.
Determine : Maximum value obtained by cutting up rod and selling pieces.
*/

public class RodCuttingCode {
public static void main(String[] args) {
int[] length = {1,2,3,4,5,6,7,8};
int[] price = {1,5,8,9,10,17,17,20};
int rodLen = 8;
int[][] dp = new int[length.length + 1][rodLen + 1];

// initializing 1st row and 1st col as 0

for (int i = 1; i < length.length + 1; i++) {
for (int j = 1; j < rodLen + 1; j++) {
int p = price[i - 1];
// int l = length[i - 1]; --> i
if(i <= j) { // valid cond
dp[i][j] = Math.max(p + dp[i][j - i], dp[i - 1][j]);
} else {
dp[i][j] = dp[i - 1][j];
}
}
}
System.out.println(dp[rodLen][rodLen]);
}
}