Skip to content

Commit f96dc95

Browse files
author
mission-peace
committed
Adding DP questions
1 parent da7c73d commit f96dc95

34 files changed

Lines changed: 1861 additions & 0 deletions
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.interview.dynamic;
2+
3+
/**
4+
* http://www.geeksforgeeks.org/dynamic-programming-set-18-word-wrap/
5+
*/
6+
public class AdjustWordsInLine {
7+
8+
public String adjust(String words[],int width){
9+
int cost[][] = new int[words.length][words.length];
10+
int split[][] = new int[words.length][words.length];
11+
12+
for(int i=0; i < words.length; i++){
13+
int diff = width - words[i].length();
14+
cost[i][i] = diff*diff;
15+
split[i][i] = i;
16+
}
17+
18+
for(int l = 2; l <= words.length; l++){
19+
for(int i=0; i < words.length - l + 1; i++){
20+
int j = i + l -1;
21+
int diff = width - words[i].length();
22+
int k;
23+
for(k = i+1 ; k <= j; k++){
24+
diff = diff - words[k].length()-1;
25+
if(diff < 0){
26+
break;
27+
}
28+
}
29+
if(k == (j+1) && diff >=0){
30+
cost[i][j] = diff*diff;
31+
split[i][j] = j;
32+
33+
}else{
34+
cost[i][j] = 100000;
35+
split[i][j] = -1;
36+
}
37+
for(k=i; k < j ; k++){
38+
if(cost[i][k] + cost[k+1][j] < cost[i][j]){
39+
cost[i][j] = cost[i][k] + cost[k+1][j];
40+
split[i][j] = k;
41+
}
42+
}
43+
}
44+
}
45+
StringBuffer buffer = new StringBuffer();
46+
int i = 0;
47+
int j = words.length-1;
48+
while(i <= j){
49+
int k = split[i][j];
50+
for(int l = i; l <= k ; l++){
51+
buffer.append(words[l] + " ");
52+
}
53+
i = k+1;
54+
buffer.append("\n");
55+
}
56+
return buffer.toString();
57+
}
58+
59+
public static void main(String args[]){
60+
String words[] = {"Tushar","Roy","Ani","Sinha"};
61+
AdjustWordsInLine awl = new AdjustWordsInLine();
62+
System.out.println(awl.adjust(words, 9));
63+
}
64+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.interview.dynamic;
2+
3+
/**
4+
* http://www.geeksforgeeks.org/dynamic-programming-set-15-longest-bitonic-subsequence/
5+
*/
6+
public class BitonicSequence {
7+
8+
public int longestSequence(int arr[]){
9+
int lis[] = new int[arr.length];
10+
int lds[] = new int[arr.length];
11+
for(int i=0; i < arr.length; i++){
12+
lis[i] = 1;
13+
lds[i] = 1;
14+
}
15+
for(int i=1 ; i < arr.length; i++){
16+
for(int j=0; j < i ; j++){
17+
if(arr[i] > arr[j]){
18+
lis[i] = Math.max(lis[i], lis[j] + 1);
19+
}
20+
}
21+
}
22+
23+
for(int i = arr.length-2; i >=0 ; i--){
24+
for(int j = arr.length-1; j > i; j--){
25+
if(arr[i] > arr[j]){
26+
lds[i] = Math.max(lds[i], lds[j] + 1);
27+
}
28+
}
29+
}
30+
int max = 0;
31+
for(int i=0; i < arr.length; i++){
32+
if(max < lis[i] + lds[i]-1){
33+
max = lis[i] + lds[i] -1;
34+
}
35+
}
36+
return max;
37+
}
38+
39+
public static void main(String args[]){
40+
BitonicSequence bs = new BitonicSequence();
41+
int[] arr = {1,4,3,7,2,1,8,11,13,0};
42+
int r = bs.longestSequence(arr);
43+
System.out.print(r);
44+
45+
}
46+
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package com.interview.dynamic;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
public class BreakMultipleWordsWithNoSpaceIntoSpace {
7+
8+
public String breakWord(char[] str,int low,Set<String> dictionary){
9+
10+
StringBuffer buff = new StringBuffer();
11+
for(int i= low; i < str.length; i++){
12+
buff.append(str[i]);
13+
if(dictionary.contains(buff.toString())){
14+
String result = breakWord(str, i+1, dictionary);
15+
if(result != null){
16+
return buff.toString() + " " + result;
17+
}
18+
}
19+
}
20+
if(dictionary.contains(buff.toString())){
21+
return buff.toString();
22+
}
23+
return null;
24+
}
25+
26+
public String dynamic(char str[], Set<String> dictionary){
27+
28+
int[][] T = new int[str.length][str.length];
29+
for(int i=0; i < T.length; i++){
30+
for(int j=0; j < T[i].length; j++){
31+
if(i == j && dictionary.contains(str[i])){
32+
T[i][j] = i;
33+
}else{
34+
T[i][j] = -1;
35+
}
36+
}
37+
}
38+
StringBuffer buff = new StringBuffer();
39+
40+
for(int l = 2; l <= str.length; l++){
41+
for(int i = 0; i <= str.length-l; i++){
42+
int j = i + l-1;
43+
buff.append(str[i]);
44+
int k;
45+
for(k=i+1; k <=j; k++){
46+
if(dictionary.contains(buff.toString()) && T[k][j] != -1){
47+
// T[i][j] = k;
48+
break;
49+
}
50+
buff.append(str[k]);
51+
}
52+
if(dictionary.contains(buff.toString())){
53+
T[i][j] = k;
54+
}
55+
buff.delete(0, buff.length());
56+
}
57+
}
58+
int low =0;
59+
int high = str.length-1;
60+
StringBuffer buffer = new StringBuffer();
61+
while(true){
62+
int r = T[low][high];
63+
if(r==-1){
64+
return null;
65+
}
66+
buffer.append(get(str,low,r)).append(" ");
67+
low = r;
68+
if(low == str.length){
69+
break;
70+
}
71+
}
72+
return buffer.toString();
73+
}
74+
75+
/**
76+
* Easier implementation to understand
77+
* Gives preference to longer words over splits
78+
* e.g peanutbutter with dict{pea nut butter peanut} it would result in
79+
* peanut butter instead of pea nut butter. Above method does the opposite
80+
* @return
81+
*/
82+
public String breakWordDP(String word, Set<String> dict){
83+
int T[][] = new int[word.length()][word.length()];
84+
85+
for(int i=0; i < T.length; i++){
86+
for(int j=0; j < T[i].length ; j++){
87+
T[i][j] = -1;
88+
}
89+
}
90+
91+
for(int l = 1; l <= word.length(); l++){
92+
for(int i=0; i < word.length() -l + 1 ; i++){
93+
int j = i + l-1;
94+
String str = word.substring(i,j+1);
95+
if(dict.contains(str)){
96+
T[i][j] = i;
97+
continue;
98+
}
99+
for(int k=i+1; k <= j; k++){
100+
if(T[i][k-1] != -1 && T[k][j] != -1){
101+
T[i][j] = k;
102+
break;
103+
}
104+
}
105+
}
106+
}
107+
if(T[0][word.length()-1] == -1){
108+
return null;
109+
}
110+
StringBuffer buffer = new StringBuffer();
111+
int i = 0; int j = word.length() -1;
112+
while(i < j){
113+
int k = T[i][j];
114+
if(i == k){
115+
buffer.append(word.substring(i, j+1));
116+
break;
117+
}
118+
buffer.append(word.substring(i,k) + " ");
119+
i = k;
120+
}
121+
122+
return buffer.toString();
123+
}
124+
125+
126+
private String get(char[] str,int low,int high){
127+
StringBuffer b = new StringBuffer();
128+
for(int i=low; i < high; i++){
129+
b.append(str[i]);
130+
}
131+
return b.toString();
132+
}
133+
134+
public static void main(String args[]){
135+
Set<String> dictionary = new HashSet<String>();
136+
dictionary.add("I");
137+
dictionary.add("like");
138+
dictionary.add("had");
139+
dictionary.add("play");
140+
dictionary.add("to");
141+
String str = "Ihadliketoplay";
142+
BreakMultipleWordsWithNoSpaceIntoSpace bmw = new BreakMultipleWordsWithNoSpaceIntoSpace();
143+
String result = bmw.dynamic(str.toCharArray(), dictionary);
144+
String result1 = bmw.breakWordDP(str, dictionary);
145+
146+
System.out.println(result);
147+
System.out.print(result1);
148+
}
149+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.interview.dynamic;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
http://www.geeksforgeeks.org/dynamic-programming-set-7-coin-change/
8+
*/
9+
public class CoinChanging {
10+
11+
public int numberOfSolutions(int total, int arr[]){
12+
13+
int temp[][] = new int[arr.length+1][total+1];
14+
15+
for(int i=0; i <= arr.length; i++){
16+
temp[i][0] = 1;
17+
}
18+
19+
for(int i=1; i <= arr.length; i++){
20+
for(int j=1; j <= total ; j++){
21+
if(arr[i-1] > j){
22+
temp[i][j] = temp[i-1][j];
23+
}
24+
else{
25+
temp[i][j] = temp[i][j-arr[i-1]] + temp[i-1][j];
26+
}
27+
}
28+
}
29+
List<Integer> result = new ArrayList<Integer>();
30+
return temp[arr.length][total];
31+
}
32+
33+
public void printCoinChangingSolution(int total,int coins[]){
34+
List<Integer> result = new ArrayList<Integer>();
35+
printActualSolution(result, total, coins, 0);
36+
}
37+
38+
private void printActualSolution(List<Integer> result,int total,int coins[],int pos){
39+
if(total == 0){
40+
for(int r : result){
41+
System.out.print(r + " ");
42+
}
43+
System.out.print("\n");
44+
}
45+
for(int i=pos; i < coins.length; i++){
46+
if(total >= coins[i]){
47+
result.add(coins[i]);
48+
printActualSolution(result,total-coins[i],coins,i);
49+
result.remove(result.size()-1);
50+
}
51+
}
52+
}
53+
54+
public int numberOfSolutionsOnSpace(int total, int arr[]){
55+
56+
int temp[] = new int[total+1];
57+
58+
temp[0] = 1;
59+
for(int i=0; i < arr.length; i++){
60+
for(int j=1; j <= total ; j++){
61+
if(j >= arr[i]){
62+
temp[j] += temp[j-arr[i]];
63+
}
64+
}
65+
}
66+
return temp[total];
67+
}
68+
69+
70+
public int minCoinChangeInfinteSupply(int total, int arr[]){
71+
int temp[] = new int[total+1];
72+
temp[0] = 0;
73+
for(int i=1; i <= total; i++){
74+
temp[i] = 10000;
75+
}
76+
for(int i=0; i < arr.length; i++){
77+
for(int j=1; j <= total; j++){
78+
if(j >= arr[i]){
79+
temp[j] = Math.min(temp[j], temp[j-arr[i]] +1);
80+
}
81+
}
82+
}
83+
return temp[total];
84+
}
85+
86+
87+
public static void main(String args[]){
88+
CoinChanging cc = new CoinChanging();
89+
int total =10;
90+
int coins[] = {6,1,3,4};
91+
cc.printCoinChangingSolution(total, coins);
92+
}
93+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.interview.dynamic;
2+
3+
/**
4+
* http://www.geeksforgeeks.org/count-number-binary-strings-without-consecutive-1s/
5+
* It is really a straight up fibonacci series with values
6+
* 1,2,3,5,8,13....
7+
* Look how we assign a[i] value of a[i-1] + b[i-1] and then b[i] becomes a[i]
8+
*/
9+
public class CountNumberOfBinaryWithoutConsecutive1s {
10+
11+
public int count(int n){
12+
int a[] = new int[n];
13+
int b[] = new int[n];
14+
15+
a[0] = 1;
16+
b[0] = 1;
17+
18+
for(int i=1; i < n; i++){
19+
a[i] = a[i-1] + b[i-1];
20+
b[i] = a[i-1];
21+
}
22+
23+
return a[n-1] + b[n-1];
24+
}
25+
26+
public static void main(String args[]){
27+
CountNumberOfBinaryWithoutConsecutive1s cnb = new CountNumberOfBinaryWithoutConsecutive1s();
28+
System.out.println(cnb.count(5));
29+
}
30+
}

0 commit comments

Comments
 (0)