-
Notifications
You must be signed in to change notification settings - Fork 22
/
k Sum.java
executable file
·73 lines (59 loc) · 2 KB
/
k Sum.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
H
1516774576
tags: DP
DP. 公式如何想到, 还需要重新理解.
dp[i][j][m]: # of possibilities such that from j elements, pick m elements and sum up to i.
i: [0~target]
dp[i][j][m] = dp[i][j-1][m] + dp[i - A[j - 1]][j-1][m-1]
(i not included) (i included)
```
/*
Given n distinct positive integers, integer k (k <= n) and a number target.
Find k numbers where sum is target. Calculate how many solutions there are?
Given [1,2,3,4], k = 2, target = 5.
There are 2 solutions: [1,4] and [2,3].
Return 2.
*/
/*
Thoughts:
Once learned the equation, it becomes easy:
create dp[i][j][m]: # of possibilities such that from j elements, pick m elements and sum up to i.
i: [0~target]
HOWEVER: need to figure out how to come up with the equation
Two ways to reach dp[i][j][m]:
If element i is not included; if i is included.
dp[i][j][m] = dp[i][j-1][m] + dp[i - A[j - 1]][j-1][m-1]
(i not included) (i included)
Initialization
dp[0][j][0], j=[0, A.length]: from j elemnts, pick 0 element to sum up to 0, there can be just 1 possibility, don't pick any thing: []
Therefore: dp[0][j][0] = 1;
*/
public class Solution {
/*
* @param A: An integer array
* @param k: A positive integer (k <= length(A))
* @param target: An integer
* @return: An integer
*/
public int kSum(int[] A, int k, int target) {
if (A == null || A.length == 0 || k <= 0) {
return 0;
}
final int[][][] dp = new int[target + 1][A.length + 1][k + 1];
for (int j = 0; j <= A.length; j++) {
dp[0][j][0] = 1;
}
for (int i = 1; i <= target; i++) {
for (int j = 1; j <= A.length; j++) {
for (int m = 1; m <= j && m <= k; m++) {
dp[i][j][m] = dp[i][j - 1][m];
if (i - A[j - 1] >= 0) {
dp[i][j][m] += dp[i - A[j - 1]][j - 1][m - 1];
}
}
}
}
return dp[target][A.length][k];
}
}
```