Skip to content

Commit 5a831bf

Browse files
Add files via upload
1 parent b148205 commit 5a831bf

7 files changed

+315
-0
lines changed

DP-2/EDIT DISTANCE.txt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
public class Solution {
2+
3+
public static int editDistance(String s, String t) {
4+
//Your code goes here
5+
//Find the lengths of both strings
6+
int m=s.length();
7+
int n=t.length();
8+
9+
int[][] dp = new int[m+1][n+1];
10+
//Initializing dp for iterative approach
11+
for (int i=n;i>=0;i--)
12+
dp[m][i]=n-i;
13+
14+
for (int i=m;i>=0;i--)
15+
dp[i][n]=m-i;
16+
17+
for (int i=m-1;i>=0;i--)
18+
{
19+
for (int j=n-1;j>=0;j--)
20+
{
21+
if (s.charAt(i)==t.charAt(j))
22+
{
23+
dp[i][j]=dp[i+1][j+1];
24+
}
25+
else
26+
{
27+
int ans1=1+dp[i+1][j+1];
28+
int ans2=1+dp[i][j+1];
29+
int ans3=1+dp[i+1][j];
30+
31+
dp[i][j]=Math.min(ans1,Math.min(ans2,ans3));
32+
}
33+
}
34+
}
35+
return dp[0][0];
36+
}
37+
38+
}

DP-2/LCS.txt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
public class Solution {
2+
3+
public static int lcs(String s, String t) {
4+
//Your code goes here
5+
int[][] dp = new int[s.length()+1][t.length()+1];
6+
for (int i=0;i<dp.length;i++)
7+
{
8+
for (int j=0;j<dp[0].length;j++)
9+
{
10+
dp[i][j]=-1;
11+
}
12+
}
13+
return lcsHelper(s,0,t,0,dp);
14+
}
15+
16+
private static int lcsHelper(String s, int i, String t, int j, int[][] dp)
17+
{
18+
if (i==s.length() || j== t.length())
19+
{
20+
return 0;
21+
}
22+
23+
if (s.charAt(i)==t.charAt(j))
24+
{
25+
if (dp[i+1][j+1]==-1)
26+
{
27+
dp[i+1][j+1]=lcsHelper(s,i+1,t,j+1,dp);
28+
}
29+
dp[i][j]=1+dp[i+1][j+1];
30+
}
31+
else
32+
{
33+
if(dp[i+1][j]==-1)
34+
{
35+
dp[i+1][j]=lcsHelper(s,i+1,t,j,dp);
36+
}
37+
int ans1=dp[i+1][j];
38+
39+
if(dp[i][j+1]==-1)
40+
{
41+
dp[i][j+1]=lcsHelper(s,i,t,j+1,dp);
42+
}
43+
int ans2=dp[i][j+1];
44+
45+
dp[i][j]=Math.max(ans1,ans2);
46+
}
47+
return dp[i][j];
48+
}
49+
50+
}

DP-2/MAGIC GRID.txt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
public class Solution{
2+
3+
4+
public static int getMinimumStrength(int[][] grid) {
5+
6+
/* Your class should be named Solution
7+
* Don't write main().
8+
* Don't read input, it is passed as function argument.
9+
* Return output and don't print it.
10+
* Taking input and printing output is handled automatically.
11+
*/
12+
int row=grid.length;
13+
if (row==0)
14+
return row;
15+
16+
int col=grid[0].length;
17+
if (col==0)
18+
return col;
19+
20+
int[][] dp=new int[row][col];
21+
dp[row-1][col-1]=1;
22+
23+
for (int i=col-2;i>=0;i--)
24+
{
25+
dp[row-1][i]=dp[row-1][i+1]-grid[row-1][i];
26+
27+
}
28+
for (int i=row-2;i>=0;i--)
29+
{
30+
dp[i][col-1]=dp[i+1][col-1]-grid[i][col-1];
31+
}
32+
33+
34+
for(int i=row-2;i>=0;i--)
35+
{
36+
for (int j=col-2;j>=0;j--)
37+
{
38+
int ans1=dp[i+1][j];
39+
int ans2=dp[i][j+1];
40+
41+
dp[i][j]=Math.max(1,Math.min(ans1,ans2)-grid[i][j]);
42+
}
43+
}
44+
45+
return dp[0][0];
46+
47+
}
48+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
public class Solution {
2+
3+
4+
public static int findMaxSquareWithAllZeros(int[][] input){
5+
6+
/* Your class should be named Solution.
7+
* Don't write main() function.
8+
* Don't read input, it is passed as function argument.
9+
* Return output and don't print it.
10+
* Taking input and printing output is handled automatically.
11+
*/
12+
int m=input.length;
13+
if (m==0)
14+
return 0;
15+
16+
int n=input[0].length;
17+
if (n==0)
18+
return 0;
19+
20+
int[][] dp = new int[m][n];
21+
int maxVal=0;
22+
23+
for (int i=0;i<n;i++)
24+
{
25+
if (input[0][i]==0)
26+
{
27+
dp[0][i]=1;
28+
}
29+
}
30+
31+
for (int i=0;i<m;i++)
32+
{
33+
if (input[i][0]==0)
34+
{
35+
dp[i][0]=1;
36+
}
37+
}
38+
39+
for (int i=1;i<m;i++)
40+
{
41+
for (int j=1;j<n;j++)
42+
{
43+
if(input[i][j]==0)
44+
{
45+
int ans1=dp[i-1][j];
46+
int ans2=dp[i][j-1];
47+
int ans3=dp[i-1][j-1];
48+
49+
dp[i][j]=Math.min(ans1,Math.min(ans2,ans3))+1;
50+
}
51+
if (dp[i][j]>maxVal)
52+
maxVal=dp[i][j];
53+
}
54+
55+
}
56+
57+
return maxVal;
58+
59+
}
60+
}

DP-2/MIN COST PATH PROBLEM.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
public class Solution {
2+
3+
public static int minCostPath(int[][] cost) {
4+
//Find number of rows m and number of cols n
5+
int m=cost.length;
6+
int n=cost[0].length;
7+
8+
//Create (m+1) * (n+1) dp matrix with all values = infinity
9+
int[][] dp = new int[m+1][n+1];
10+
for (int i=0;i<dp.length;i++)
11+
{
12+
for (int j=0;j<dp[0].length;j++)
13+
{
14+
//We initialize each element as -infinity as the cost can be any positive or negative value (including +infinity)
15+
dp[i][j]=Integer.MAX_VALUE;
16+
}
17+
}
18+
19+
//We start filling the minimum cost values for elements from (m-1,n-1) to (0,0)
20+
for (int i=m-1;i>=0;i--)
21+
{
22+
for (int j=n-1;j>=0;j--)
23+
{
24+
//Handle special case for i=m-1, j=n-1
25+
if (i==m-1 && j==n-1)
26+
{
27+
dp[i][j]=cost[i][j];
28+
continue;
29+
}
30+
int ans1=dp[i+1][j];
31+
int ans2=dp[i][j+1];
32+
int ans3=dp[i+1][j+1];
33+
34+
dp[i][j]=cost[i][j]+Math.min(ans1, Math.min(ans2, ans3));
35+
}
36+
}
37+
return dp[0][0];
38+
}
39+
}

DP-2/SMALLEST SUPER-SEQUENCE.txt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
public class Solution {
2+
3+
public static int smallestSuperSequence(String str1, String str2) {
4+
5+
/* Your class should be named Solution
6+
* Don't write main().
7+
* Don't read input, it is passed as function argument.
8+
* Return output and don't print it.
9+
* Taking input and printing output is handled automatically.
10+
*/
11+
int n1=str1.length();
12+
int n2=str2.length();
13+
14+
int[][] dp = new int[n1+1][n2+1];
15+
for (int i=n1;i>=0;i--)
16+
{
17+
dp[i][n2]=n1-i;
18+
}
19+
for (int i=n2;i>=0;i--)
20+
{
21+
dp[n1][i]=n2-i;
22+
}
23+
24+
for (int i=n1-1;i>=0;i--)
25+
{
26+
for (int j=n2-1;j>=0;j--)
27+
{
28+
if(str1.charAt(i)==str2.charAt(j))
29+
{
30+
dp[i][j]=dp[i+1][j+1]+1;
31+
}
32+
else
33+
{
34+
35+
int ans1=dp[i+1][j];
36+
int ans2=dp[i][j+1];
37+
38+
dp[i][j]=Math.min(ans1,ans2)+1;
39+
}
40+
}
41+
}
42+
43+
return dp[0][0];
44+
45+
}
46+
}

DP-2/WAYS TO MAKE COIN CHANGE.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
public class Solution {
2+
3+
4+
public static int countWaysToMakeChange(int denominations[], int value){
5+
// Write your code here
6+
/*
7+
if (value==0)
8+
return 1;
9+
10+
if (value<0)
11+
return 0;
12+
13+
int finalAns=0;
14+
for (int i=0;i<denominations.length;i++)
15+
{
16+
finalAns=finalAns+countWaysToMakeChange(denominations,value-denominations[i]);
17+
}
18+
return finalAns;
19+
*/
20+
int[] dp = new int[value+1];
21+
dp[0]=1;
22+
for (int i=0;i<denominations.length;i++)
23+
{
24+
for (int j=0;j<=value;j++)
25+
{
26+
if (j>=denominations[i])
27+
{
28+
dp[j]=dp[j]+dp[j-denominations[i]];
29+
}
30+
}
31+
}
32+
return dp[value];
33+
}
34+
}

0 commit comments

Comments
 (0)