dymanic problems
COIN Changing
coins 1 2 3 total 5
1 1 1 1 1 one way to get 5
1 1 1 2 2 way to get 5
1 1 3
2 3
1 2 2
j
1 1 1 1 1 one way to get 5
1 1 1 2 2 way to get 5
1 1 3
2 3
1 2 2
j
i/j |
0 |
1 |
2 |
3 |
4 |
5 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
2 |
1 |
1 |
2 |
2 |
3 |
3 |
3 |
1 |
1 |
2 |
3 |
4 |
5 |
if j>= coins[i]
T[i][j] = T[i-1][j] + T[i][j-coins[i]];
else
T[i][j] = T[i-1][j] ;
public int numberOfSolutions(int total, int coins[]){ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
int temp[][] = new int[coins.length+1][total+1]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for(int i=0; i <= coins.length; i++){ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
temp[i][0] = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for(int i=1; i <= coins.length; i++){ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for(int j=1; j <= total ; j++){ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if(coins[i-1] > j){ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
temp[i][j] = temp[i-1][j]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else{ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
temp[i][j] = temp[i][j-coins[i-1]] + temp[i-1][j]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return temp[coins.length][total]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}Longest Increasing Subsequence
If arr[j]<arr[i]T[i]=max(T[i],T[j]+1)https://github.com/mission-peace/interview/blob/master/src/com/interview/dynamic/LongestIncreasingSubsequence.javaLongest Common Subsequence
if(str[i]==str[j])
|
(if element of last row & last column is TRUE then sum exists)
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
|
2 |
T |
F |
T |
F |
F |
F |
F |
F |
F |
F |
F |
F |
3 |
T |
F |
T |
T |
F |
T |
F |
F |
F |
F |
F |
F |
7 |
T |
F |
T |
T |
F |
T |
F |
T |
F |
T |
T |
F |
8 |
T |
F |
T |
T |
F |
T |
F |
T |
T |
T |
T |
T |
10 |
T |
F |
T |
T |
F |
T |
F |
T |
T |
T |
T |
T |
Answer (8,3)
if j>= input[i]
T[i][j] = T[i-1][j] ;
else
T[i][j] = T[i-1][j] T[i-1][j-input[i]] ;
* Space complexity - O(input.size*total_sum) | |||
* | |||
| |||
public class SubsetSum { | |||
public boolean subsetSum(int input[], int total) { | |||
boolean T[][] = new boolean[input.length + 1][total + 1]; | |||
for (int i = 0; i <= input.length; i++) { | |||
T[i][0] = true; | |||
} | |||
for (int i = 1; i <= input.length; i++) { | |||
for (int j = 1; j <= total; j++) { | |||
if (j - input[i - 1] >= 0) { (go input[i] steps back) | |||
T[i][j] = T[i - 1][j] || T[i - 1][j - input[i - 1]]; | |||
} else { | |||
T[i][j] = T[i-1][j]; (copy from up row) | |||
} | |||
} | |||
} | |||
return T[input.length][total]; | |||
} | |||
public boolean partition(int arr[]) { | |||
int sum = 0; | |||
for (int i = 0; i < arr.length; i++) { | |||
sum += arr[i]; | |||
} | |||
if (sum % 2 != 0) { | |||
return false; | |||
} | |||
sum = sum / 2; | |||
boolean[][] T = new boolean[arr.length + 1][sum + 1]; | |||
for (int i = 0; i <= arr.length; i++) { | |||
T[i][0] = true; | |||
} | |||
for (int i = 1; i <= arr.length; i++) { | |||
for (int j = 1; j <= sum; j++) { | |||
if (j - arr[i - 1] >= 0) { | |||
T[i][j] = T[i - 1][j - arr[i - 1]] || T[i - 1][j]; | |||
} else { | |||
T[i][j] = T[i-1][j]; | |||
} | |||
} | |||
} | |||
return T[arr.length][sum]; | |||
} | |||
public static void main(String args[]) { | |||
SubsetSum ss = new SubsetSum(); | |||
int arr[] = {1, 3, 5, 5, 2, 1, 1, 6}; | |||
System.out.println(ss.partition(arr)); | |||
int arr1[] = {2, 3, 7, 8}; | |||
System.out.print(ss.subsetSum(arr1, 11)); | |||
} | |||
} |
MINIMUM EDIT DISTANCE :
a |
b |
c |
d |
e |
F |
||
0 |
1 |
2 |
3 |
4 |
5 |
6 |
|
a |
1 |
0 |
1 |
2 |
3 |
4 |
5 |
z |
2 |
1 |
1 |
2 |
3 |
4 |
5 |
c |
3 |
2 |
2 |
1 |
2 |
3 |
4 |
e |
4 |
3 |
3 |
2 |
2 |
2 |
3 |
d |
5 |
4 |
4 |
3 |
2 |
3 |
3 |
Operations :
F->d
d (delete)
b->z
Formula :
if(str1[i]==str[j])
T[I][J]==T[i-1][j-1]
else
T[I][J]==min(T[i-1][j],T[i-1][j-1],T[i][j-1])+1
https://github.com/mission-peace/interview/blob/master/src/com/interview/dynamic/EditDistance.javaCutting Rod to maximize profit
rod |
1 |
2 |
3 |
4 |
price |
2 |
5 |
7 |
8 |
i/j | 0 |
1 |
2 |
3 |
4 |
5 |
|
2 |
1 |
0 |
2 |
4 |
6 |
8 |
10 |
5 |
2 |
0 |
2 |
5 |
7 |
10 |
12 |
7 |
3 |
0 |
2 |
5 |
7 |
10 |
12 |
8 |
4 |
0 |
2 |
5 |
7 |
10 |
12 |
Answer 1,2,2
Formula:
if(j>=i)
T[i][j]=max(T[i-1][j]+T[i-1][j-i])
else
T[ii][j]=T[i-1][j]
PICK FROM EITHER SIDE TO MAXIMIZE VALUE
3 9 1 21 |
2 |
3 |
4 |
|
1 |
f,s(3,0) |
(9,3) |
(4,9) |
(11,4) |
2 |
(9,0) |
(9,1) |
(10,2) |
|
3 |
(1,0) |
(2,1) |
||
4 |
(2,0) |
1 |
2 |
3 |
4 |
3 |
9 |
1 |
2 |
T[i][j]=max(T[i+1][j] second+ val[i]), T[i][j-1] second+ val[j])
T[i][j].second=max(T[i+1][j] first or val[i]), T[i][j-1] first)
https://github.com/mission-peace/interview/blob/master/src/com/interview/dynamic/NPotGold.javaCutting Rod to maximize profit
rod |
1 |
2 |
3 |
4 |
price |
2 |
5 |
7 |
8 |
i/j | 0 |
1 |
2 |
3 |
4 |
5 |
|
2 |
1 |
0 |
2 |
4 |
6 |
8 |
10 |
5 |
2 |
0 |
2 |
5 |
7 |
10 |
12 |
7 |
3 |
0 |
2 |
5 |
7 |
10 |
12 |
8 |
4 |
0 |
2 |
5 |
7 |
10 |
12 |
Answer 1,2,2
Formula:
if(j>=i)
T[i][j]=max(T[i-1][j]+T[i-1][j-i])
else
T[ii][j]=T[i-1][j]
Word Break Problem :
GIVEN A STRING & DICTIONARY YOU HAVE TO TELL WHETHER EACH WORDS IN A STRING BELONGS TO DICTIONARY
(IAMC SHOULD RETURN FALSE)I |
a |
m |
a |
c |
e |
0 |
1 |
2 |
3 |
4 |
5 |
i/j | 0 |
1 |
2 |
3 |
4 |
5 |
0 |
T |
T |
T(0) |
T(1) |
F |
T(0) |
1 |
T |
T |
T(2) |
F |
T(2) |
|
2 |
F |
F |
F |
F |
||
3 |
T |
F |
T(3-5) |
|||
4 |
F |
F |
||||
5 |
F |
Answer I, AM, ACE
Formula:
if(INPUT[I--J] IS IN DICTIONARY
T[i][j]=TRUE
else
T[i][j]=TRUE IF(T[I][K] && T[K+1][J])==TRUE
EGG DROPPING PROBLEM
GIVEN CERTAIN NUMBER OF FLOORS & EGGS find from which floor egg will break
egg/floors |
1 |
2 |
3 |
4 |
5 |
6 |
1 |
1 |
2 |
3 |
4 |
5 |
6 |
2 |
1 |
2 |
2 |
3 |
3 |
3 |
floors 1 2 3 4 5 6min(1+max(if it breaks,if it doesnt break))1 + max(0,3)= 4 (floor 1)1 + max(1,3)= 4 (floor 2)1 + max(2,2)= 3 (floor 3)1 + max(3,2)= 4 (floor 4)1 + max(4,1)= 5 (floor 5)1 + max(5,0)= 6 (floor 5) |
T[i][j]= T[i-1][j]
else
T[i][j]=1+min(max(T[i-1][k-1],T[i][j-k])
where k is 1...j
Matrix Chain Multiplication
GIVEN CERTAIN NUMBER what order should multiply them such as multiplication is minimum remember
0 |
1 |
2 |
3 |
[2,3] |
[3,6] |
[6,4] |
[4,5] |
l=372+(2*3*4) = 9636+(2*6*4) = 84l=4132+(2*3*5)=16236+120+2*6*5=26084+(2*4*5)=124
|
Comments
Post a Comment