-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy path#SolutionsOfLinearEqtn.java
92 lines (66 loc) · 2.84 KB
/
#SolutionsOfLinearEqtn.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
https://www.techiedelight.com/total-possible-solutions-linear-equation-k-variables/
Problem is similar to the problem - number of ways to change coin.
We either include the current coefficient or exlcude it.
Recurrence -
count(coeff, k, rhs) = count(coeff, k, rhs-coeff[k]) + count(coeff, k-1, rhs)
where, count(coeff, k, rhs-coeff[k]) --> including coeff[k]
count(coeff, k-1, rhs) --> excluding coeff[k]
*/
//Recursion
public int count(int coeff[], int index, int rhs){
//Base Case: we have considered valid coeff and their summation equals rhs
if(rhs == 0) return 1;
//Base Case: we have considered invalid coeff and their summation does not equals rhs
else if(rhs<0 || index< 0) return 0;
//Include the current coeff
int includeIndex = count(coeff, index, rhs-coeff[index]);
//Exclude the current coeff
int excludeIndex = count(coeff, index-1, rhs);
return include + exclude;
}
//Memoized Recursion
public int count(int coeff[], int index, int rhs, HashMap<String, Integer> map){
//Base Case: we have considered valid coeff and their summation equals rhs
if(rhs == 0) return 1;
//Base Case: we have considered invalid coeff and their summation does not equals rhs
else if(rhs<0 || index< 0) return 0;
String key = index + "|" + rhs;
if(!map.containsKey(key)){
//Include the current coeff
int includeIndex = count(coeff, index, rhs-coeff[index]);
//Exclude the current coeff
int excludeIndex = count(coeff, index-1, rhs);
map.put(key, include + exclude);
}
return map.get(key);
}
//Bottom up DP
public int count(int coeff[], int rhs){
int numOfCoeff = coeff.length;
//dp[i] indicates the number of ways to form a sum, sum = i
int dp[] = new int[numOfCoeff+1];
dp[0] = 1;
/*
we consider all coefficients in the range [0...i] when computing dp[]
for first interation i = 0 --> only first coeff is considered
for second iteration i = 1 --> first two coeficients are considered [0,1]
for last interation i = numOfCoeff --> all coefficients are considered
We basically answer the question -
#ways to sum up to j when we conisder only the first coefficient?
#ways to sum up to j when we conisder the first two coefficients?
.....
#ways to sum up to RHS when we consider all coefficients
*/
for(int i=0 ; i < numOfCoeff ; i++){
for(int j=coeff[i] ; j <= rhs ; j++){
//since we are including coeff[i] in sum = j, we must add #ways to get a sum = (j-coeff[i])
dp[j] += dp[ j - coeff[i] ];
}
}
return dp[rhs];
}
/*
Time Complexity - O(numberOfCoefficients * RHS)
Pseudo Polynomial
*/