-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Pranav Anil Bhole
committed
Nov 14, 2020
1 parent
a80d444
commit f43f0e4
Showing
5 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/com/bhole/advanced_ds/dp/basic/EggDroppingPuzzle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/bhole/advanced_ds/dp/basic/MatrixChainMultiplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/test/java/com/bhole/advanced_ds/dp/basic/EggDroppingPuzzleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/test/java/com/bhole/advanced_ds/dp/basic/MatrixChainMultiplicationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
} | ||
|