-
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 15, 2020
1 parent
f43f0e4
commit 9af5df0
Showing
4 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
src/main/java/com/bhole/advanced_ds/dp/basic/RodCutting.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,30 @@ | ||
package com.bhole.advanced_ds.dp.basic; | ||
|
||
public class RodCutting { | ||
/* | ||
length | 1 2 3 4 5 6 7 8 | ||
-------------------------------------------- | ||
price | 1 5 8 9 10 17 17 20 | ||
Recursive approach of rod cutting with memoized version has exponential complexity and can run out of stack. | ||
Better approach is store subproblems in 2d array. | ||
Row denotes piece lengths, Column has 0 to N available length. | ||
It works on traversing from top left to bottom right, and store the max of current+subproblems, subproblem of previous piece with same col length. | ||
*/ | ||
|
||
public int solve(int price[], int n) { | ||
if(price.length ==0) return 0; | ||
int dp[][] = new int[price.length][n+1]; | ||
for (int i=1; i<price.length; i++) { | ||
for (int j=1; j<=n; j++) { | ||
if (i > j) { | ||
dp[i][j] = dp[i-1][j]; | ||
} else { | ||
dp[i][j] = Math.max(dp[i][j-i] + price[i], dp[i-1][j]); | ||
} | ||
} | ||
} | ||
return dp[price.length-1][n]; | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/com/bhole/advanced_ds/dp/basic/WordBreakingProblem.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,49 @@ | ||
package com.bhole.advanced_ds.dp.basic; | ||
|
||
import java.util.List; | ||
|
||
//WIP | ||
public class WordBreakingProblem { | ||
/* | ||
given string and dictionary, find out if the string can be broken down into words in dictionary. | ||
For example: Iamace, dictionary: {I, am, ace} | ||
This problem can be solved using DP, rows: str len N, col: str len N; | ||
We only fill upper diagonal metrix, with length of words from 1 to Str Len N. | ||
for all diagonals i,i, we check if the there is word string at i in dictionary, | ||
for each word length, | ||
we do partitioning and check if the subproblem from i to k-1, and k to j is exists, | ||
if exists then make current as true | ||
else move on to next partitioning. | ||
*/ | ||
|
||
public boolean solve(String str, List<String> dictionary) { | ||
if (str == null || dictionary.isEmpty()) return false; | ||
str = str.trim().toLowerCase(); | ||
if (str.length() == 0) return false; | ||
boolean dp[][] = new boolean[str.length()][str.length()]; | ||
for (int i=0; i<str.length(); i++) { | ||
char c = str.charAt(i); | ||
dp[i][i] = dictionary.stream().anyMatch(word-> word.startsWith(""+c)); //can be replaced with TRIE Log N lookup | ||
} | ||
for (int len=2; len<=str.length(); len++) { | ||
for (int i=0; i<str.length()-len; i++) { | ||
int j= i+len-1; | ||
String word = str.substring(i, j+1); | ||
System.out.println(String.format("Len: %d, word=%s, i=%d, j=%d", len, word, i, j)); | ||
if (dictionary.contains(word)) { | ||
dp[i][j] = true; | ||
continue; | ||
} | ||
for (int k=i+1; k<=j; k++) { | ||
System.out.println(String.format("Len: %d, i=%d, k=%d, j=%d", len,i,k,j)); | ||
if (dp[i][k-1] && dp[k][j]) { | ||
dp[i][j] = true; | ||
continue; | ||
} | ||
} | ||
} | ||
} | ||
return dp[0][str.length()-1]; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/test/java/com/bhole/advanced_ds/dp/basic/RodCuttingTest.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 RodCuttingTest { | ||
@Test | ||
public void test() { | ||
RodCutting rodCutting = new RodCutting(); | ||
Assert.assertEquals(rodCutting.solve(new int[]{0, 1, 5, 8, 9, 10, 17}, 8), 22); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/test/java/com/bhole/advanced_ds/dp/basic/WordBreakingProblemTest.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; | ||
|
||
import java.util.List; | ||
|
||
public class WordBreakingProblemTest { | ||
@Test | ||
public void test() { | ||
WordBreakingProblem problem = new WordBreakingProblem(); | ||
Assert.assertEquals(problem.solve("iamace", List.of("i", "am", "ace")), true); | ||
} | ||
} |