Skip to content

Commit

Permalink
2 imp dps
Browse files Browse the repository at this point in the history
  • Loading branch information
Pranav Anil Bhole committed Nov 14, 2020
1 parent a80d444 commit f43f0e4
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@
7. 0/1 Knapsack
8. Longest Common Subsequence
9. Max Length Snake Seq
10. Egg Dropping Puzzle
11. Matrix Chain Multiplication

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.bhole.advanced_ds.dp.basic;

public class EggDroppingPuzzle {
/*
find out the minimum trials required to find out that egg will break from nth floorss,
f= number of floors
k = number of eggs
Idea is to store sub problems in 2d array, rows(i) = eggs, columns(j) = floors.
for i eggs, j floor,
try dropping egg from each floor(k= 1 to j) and minimize number of attempt.
1 + max ( eggs breaks at floors k sub problem, egg dont break at k sub problem)
for example dp[2][4]
minimize the attepts by dropping egg from floor 1 to floor 4.
1+ max( if egg breaks at 1st floor then dp[1][0] to work with, if egg does not break then we have 3 floors and 2 eggs to work with)
which is 1 + max(dp[1][0],dp[2][4-1])
*/

int solve(int floors, int eggs) {
if (floors <= 0 || eggs <=0) return 0;
int dp[][] = new int[eggs][floors+1];
for (int i=0; i<eggs; i++) {
for (int j=0; j<=floors; j++) {
if(j==0) {
dp[i][0] = 0;
}
else if(i==0) {
dp[i][j] = 1+ dp[i][j-1];
} else {
int min = Integer.MAX_VALUE;
for(int k=1; k<=j; k++) {
min = Math.min(min, 1 + Math.max(dp[i-1][k-1], dp[i][j-k]));
}
dp[i][j] = min;
}
}
}
return dp[eggs-1][floors];
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.bhole.advanced_ds.dp.basic;

public class MatrixChainMultiplication {
/*
Find min cost of multiplication of matrix.
DP solution is amazing,
it starts with batch interval of length, runs from 2 to max;
dp[i][i] diagonal will always be zero as no cost for multiplication with itself.
for length 0 to 4, it prepares the following batches of i, j, k where i< j < k
0, 1, 2
1, 2, 3
2, 3, 4 // stores min result dp[2][4] matrix multiplication from 2 to 4 index.
0, 1, 3
0, 2, 3
1, 2, 4
1, 3, 4
0, 1, 4
0, 2, 4
0, 3, 4
we fill dp of only upper diagonal, and use previously computed results from batches.
for example batch 0, 1, 4 uses dp[0][1] and dp[1][4] in min computation
*/
public int solve(int arr[]) {
int dp[][] = new int[arr.length][arr.length];
for (int len=2; len < arr.length; len++) {
for (int i=0; i<(arr.length-len); i++) {
int j = i + len;
dp[i][j] = Integer.MAX_VALUE;
for (int k=i+1; k<j; k++) {
System.out.println(String.format("%s, %s, %s", i, k, j));
dp[i][j] = Math.min(dp[i][j], dp[i][k] + dp[k][j] + (arr[i] * arr[k] * arr[j]));
}
}
}

return dp[0][arr.length-1];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.bhole.advanced_ds.dp.basic;

import org.junit.Assert;
import org.junit.Test;

public class EggDroppingPuzzleTest {
@Test
public void eggDropTest() {
EggDroppingPuzzle puzzle = new EggDroppingPuzzle();
Assert.assertEquals(puzzle.solve(6, 2), 3);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.bhole.advanced_ds.dp.basic;

import org.junit.Assert;
import org.junit.Test;

public class MatrixChainMultiplicationTest {
@Test
public void test() {
MatrixChainMultiplication matrixChain = new MatrixChainMultiplication();
Assert.assertEquals(matrixChain.solve(new int[]{40, 20, 30, 10, 30}), 26000);

}
}

0 comments on commit f43f0e4

Please sign in to comment.