Skip to content

Commit a2d8df6

Browse files
committed
added dp problems
0 parents  commit a2d8df6

File tree

9 files changed

+745
-0
lines changed

9 files changed

+745
-0
lines changed

ConvertIntoPalindrome.java

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.program.dynamic.programming;
2+
3+
/**
4+
* @author rrohit
5+
*/
6+
public class ConvertIntoPalindrome {
7+
8+
/**
9+
* This method will add characters to make the given word a palindrome.
10+
* Time : O(n+n) = O(n)
11+
* Space: O(n)
12+
* @param word
13+
* @return
14+
*/
15+
public static String convertIntoPalindrome(String word) {
16+
17+
if (word == null || word.isEmpty()) {
18+
return "a";
19+
}
20+
21+
int wordlen = word.length();
22+
if (wordlen == 1){
23+
return word;
24+
}
25+
26+
boolean match[] = new boolean[wordlen];
27+
int left = 0, right = wordlen-1, matchCount = 0;
28+
29+
while (left < right) {
30+
31+
if (word.charAt(left) == word.charAt(right)) {
32+
match[left] = match[right] = true;
33+
matchCount += 2;
34+
left++;
35+
right--;
36+
} else {
37+
match[right] = false;
38+
right--;
39+
}
40+
}
41+
42+
/**
43+
* In case word it self is a palindrome.
44+
*/
45+
if (matchCount == wordlen || matchCount == wordlen-1 && match[wordlen-1]) {
46+
return word;
47+
}
48+
int j = wordlen-1;
49+
StringBuilder sb = new StringBuilder();
50+
while (j > right) {
51+
52+
if (!match[j]) {
53+
sb.append(word.charAt(j));
54+
}
55+
j--;
56+
}
57+
58+
String palindrome = sb.append(word).toString();
59+
60+
return palindrome;
61+
62+
}
63+
64+
/**
65+
* Condition 1 : If First and Last chars should match.
66+
* Condition 2 : Substring (excluding first and last chars) must be palindrome.
67+
* @param word
68+
* @return
69+
*/
70+
public static boolean palindrome(String word) {
71+
72+
if (word == null) {
73+
return false;
74+
}
75+
76+
if (word.isEmpty() || word.length() == 1) {
77+
return true;
78+
}
79+
80+
if (word.charAt(0) != word.charAt(word.length()-1)){
81+
return false;
82+
}
83+
return palindrome(word.substring(1, word.length()-1));
84+
85+
}
86+
87+
public static void main(String[] args) {
88+
89+
//System.out.println(convertIntoPalindrome("poaop"));
90+
System.out.println(convertIntoPalindrome("madam"));
91+
}
92+
93+
}

Knapsack.java

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.program.dynamic.programming;
2+
3+
public class Knapsack {
4+
5+
private static int table[][];
6+
private static int n;
7+
/**
8+
* Time : O(N * C )
9+
* Space : O (N * C)
10+
* @param capacity
11+
* @param w
12+
* @param v
13+
* @return
14+
*/
15+
public static int knapsackSol(int capacity, int [] w, int v []) {
16+
17+
table = new int[v.length+1][capacity+1];
18+
n = w.length;
19+
20+
return knapsackDP(0, capacity, w, v);
21+
}
22+
23+
static int max(int a, int b) {
24+
return a > b ? a : b;
25+
}
26+
27+
/**
28+
* Recursive Solution.
29+
*
30+
* Time Complexity : O(2^n).
31+
* @param w
32+
* @param wt
33+
* @param val
34+
* @param n
35+
* @return
36+
*/
37+
public static int knapsack(int w, int wt[], int val[], int n) {
38+
39+
if (w == 0 || n == 0) {
40+
return 0;
41+
}
42+
43+
if (wt[n-1] > w) {
44+
return knapSack(w, wt, val, n-1);
45+
} else {
46+
return max(val[n-1] + knapSack(w - wt[n-1], wt, val, n-1),
47+
knapsack(w, wt, val, n-1));
48+
}
49+
}
50+
51+
static int knapsackDP(int i, int w, int [] wt, int []v){
52+
53+
if (i >= n) {
54+
table[i][w] = 0;
55+
return table[i][w];
56+
}
57+
58+
if (wt[i] > w)
59+
return table[i+1][w] = knapsackDP(i + 1, w, wt, v);
60+
else
61+
return table[i+1][w] = max(knapsackDP(i + 1, w, wt, v), knapsackDP(i + 1, w - wt[i], wt, v) + v[i]);
62+
63+
}
64+
65+
// Returns the maximum value that can be put in a knapsack of capacity W
66+
static int knapSack(int W, int wt[], int val[], int n) {
67+
68+
int i, w;
69+
int table[][] = new int[n+1][W+1];
70+
71+
// Build table K[][] in bottom up manner
72+
for (i = 0; i <= n; i++) {
73+
for (w = 0; w <= W; w++)
74+
{
75+
if (i==0 || w==0)
76+
table[i][w] = 0;
77+
else if (wt[i-1] <= w)
78+
table[i][w] = max(val[i-1] + table[i-1][w-wt[i-1]], table[i-1][w]);
79+
else
80+
table[i][w] = table[i-1][w];
81+
}
82+
}
83+
84+
return table[n][W];
85+
}
86+
87+
88+
89+
public static void main(String[] args) {
90+
int weight[] = new int[7];
91+
weight[0] = 9;
92+
weight[1] = 6;
93+
weight[2] = 1;
94+
weight[3] = 2;
95+
weight[4] = 5;
96+
weight[5] = 4;
97+
98+
int value[] = new int[7];
99+
value[0] = 11;
100+
value[1] = 10;
101+
value[2] = 5;
102+
value[3] = 7;
103+
value[4] = 12;
104+
value[5] = 8;
105+
106+
/*
107+
* Set all values of 2D matrix CostTable to Minus 1
108+
*/
109+
int capacity = 10;
110+
System.out.println(knapsackSol(capacity, weight, value));
111+
112+
System.out.println(knapSack(capacity, weight, value, weight.length));
113+
114+
System.out.println( knapsack(capacity, weight, value, weight.length-1));
115+
}
116+
117+
}

LongestCommonSubsequence.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.program.dynamic.programming;
2+
/*
3+
* We have two strings X = {"ABCBDAB"} and Y = {"BDCABA"}
4+
* Output must be : LCS(X, Y) = {"BCBA", "BDAB", "BCAB"}
5+
*
6+
* Solution :
7+
* i = X.length() and j = Y.lenght()
8+
* LCS(i,j),
9+
* if X[i-1] == Y[j-1], then LCS(X[i-1], Y[j-1])
10+
* if X[i-1] != Y[j-1], then Max(LCS(X[i-1], Y[j]), LCS(X[i], Y[j-1]))
11+
*
12+
* @author rrohit
13+
* */
14+
public class LongestCommonSubsequence {
15+
16+
public static int LCSLength(String X, String Y){
17+
return LCSLength(X.toCharArray(), X.length(), Y.toCharArray(), Y.length());
18+
}
19+
20+
/*
21+
* This is only Recursive Solution, very time consuming O(2^n). Now we need to add memorization
22+
* technique convert this into Dynamic Programming. And reduce the time complexity to polynomial instead of
23+
* exponential.
24+
*/
25+
private static int LCSLength(char[] X, int i, char[] Y, int j) {
26+
27+
if (i==0 || j==0){
28+
return 0;
29+
} else if (X[i-1] == Y[j-1]) {
30+
return 1 + LCSLength(X, i-1, Y, j-1);
31+
} else {
32+
return Math.max(LCSLength(X, i-1, Y, j), LCSLength(X, i, Y, j-1));
33+
}
34+
}
35+
36+
/* Dynamic Programming Solution
37+
* https://www.youtube.com/watch?v=wJ-rP9hJXO0
38+
*
39+
* Using memorization, LCS[][] = new int[m][n]; where m = X.lenth() and n = Y.length()
40+
* LCS[0][j] = 0, for all j, coz we are not considering X only taking Y
41+
* LCS[i][0] = 0, for all i, coz we are not considering Y only taking X
42+
* LCS[i][j] = 1 + LCS(i-1, j-1) if X[i-1] == Y[j-1]
43+
* LCS[i][j] = max(LCS(i-1,j), LCS(i, j-1)) if X[i-1] != Y[j-1]
44+
*
45+
* Bottom Up Construction
46+
* Time : O(m*n)
47+
* Space : O(m*n)
48+
*/
49+
public static int LCSLengthDP(char[] X, char[] Y) {
50+
int m = X.length;
51+
int n = Y.length;
52+
int [][] LCS = new int[m+1][n+1];
53+
int i,j;
54+
for (i=0; i<=m; i++){
55+
LCS[i][0] = 0;
56+
}
57+
58+
for (j=0; j<=n; j++){
59+
LCS[0][j] = 0;
60+
}
61+
62+
for (i=1; i<=m; i++){
63+
for (j=1; j<=n; j++){
64+
if (X[i-1]==Y[j-1]) {
65+
LCS[i][j] = 1 + LCS[i-1][j-1];
66+
}else{
67+
LCS[i][j] = Math.max(LCS[i-1][j], LCS[i][j-1]);
68+
}
69+
}
70+
}
71+
return LCS[m][n];
72+
}
73+
74+
public static void main(String[] args) {
75+
76+
String X = "ABCBDAB";
77+
String Y = "BDCABA";
78+
System.out.println(LCSLength(X, Y));
79+
System.out.println(LCSLengthDP(X.toCharArray(), Y.toCharArray()));
80+
}
81+
82+
83+
84+
85+
}

Pair.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.program.dynamic.programming;
2+
3+
/**
4+
* Created by rrohit on 13/10/16.
5+
*/
6+
public class Pair {
7+
8+
public static int pairCount(int arr[], int target) {
9+
10+
int pairCount = 0;
11+
if (arr.length == 0 || arr.length == 1) {
12+
return pairCount;
13+
}
14+
15+
int start = 0, end = arr.length-1;
16+
int currentSum = 0;
17+
18+
while (start < end) {
19+
20+
currentSum = arr[start] + arr[end];
21+
if (currentSum >= target) {
22+
pairCount += end - start;
23+
end--;
24+
} else {
25+
start++;
26+
}
27+
28+
29+
}
30+
return pairCount;
31+
}
32+
33+
public static void main(String args[]) {
34+
int arr[] = {2, 3, 4, 10, 12, 30, 45};
35+
System.out.println(pairCount(arr, 40));
36+
}
37+
38+
}

PascalTriangle.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.program.dynamic.programming;
2+
3+
/**
4+
* Created by rrohit on 11/10/16.
5+
*/
6+
public class PascalTriangle {
7+
8+
public static void main(String[] args) {
9+
PascalTriangle main = new PascalTriangle();
10+
main.printPascalTriangle(7);
11+
}
12+
13+
public int pascal(int i, int j) {
14+
if (j == 0 || i == j) {
15+
return 1;
16+
}
17+
return pascal(i - 1, j - 1) + pascal(i - 1, j);
18+
}
19+
20+
/**
21+
*
22+
* @param level
23+
*/
24+
public void printPascalTriangle(int level) {
25+
26+
for (int i = 0; i < level; i++) {
27+
for (int j = 0; j <= i; j++) {
28+
System.out.print("\t"+pascal(i, j)+" ");
29+
}
30+
System.out.println();
31+
}
32+
}
33+
34+
}

0 commit comments

Comments
 (0)