forked from kamyu104/LintCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
k-sum.cpp
113 lines (99 loc) · 3.13 KB
/
k-sum.cpp
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Time: O(k * n * t)
// Space: O(n * t)
class Solution {
public:
/**
* @param A: an integer array.
* @param k: a positive integer (k <= length(A))
* @param target: a integer
* @return an integer
*/
int kSum(vector<int> A, int k, int target) {
const int n = A.size();
// table[i][j][t] denotes the number of ways to select,
// from first i elements, j elements whose sum equals to t
vector<vector<vector<int>>> table(2, vector<vector<int>>(n + 1, vector<int>(target + 1, 0)));
for (int i = 1; i <= n; ++i) {
if (A[i - 1] <= target) {
for (int j = i; j <= n; ++j) {
table[1][j][A[i - 1]] = 1;
}
}
}
for (int i = 2; i <= k; ++i) {
for (int j = i; j <= n; ++j) {
for (int p = 1; p <= target; ++p) {
table[i % 2][j][p] = 0;
if (i < j) {
table[i % 2][j][p] = table[i % 2][j - 1][p];
}
if (p - A[j - 1] >= 0) {
table[i % 2][j][p] += table[(i - 1) % 2][j - 1][p - A[j - 1]];
}
}
}
}
return table[k % 2][n][target];
}
};
// Time: O(k * n * t)
// Space: O(k * n * t)
class Solution2 {
public:
/**
* @param A: an integer array.
* @param k: a positive integer (k <= length(A))
* @param target: a integer
* @return an integer
*/
int kSum(vector<int> A, int k, int target) {
const int n = A.size();
// table[i][j][t] denotes the number of ways to select,
// from first i elements, j elements whose sum equals to t
vector<vector<vector<int>>> table(n+1, vector<vector<int>>(k + 1, vector<int>(target + 1, 0)));
for (int i = 1; i <= n; ++i) {
if (A[i - 1] <= target)
{
for (int j = i; j <= n; ++j) {
table[j][1][A[i - 1]] = 1;
}
}
}
for (int i = 1; i <= n; ++i) {
for (int j = min(i, k); j > 1; --j) {
for (int p = 1; p <= target; ++p) {
table[i][j][p] = table[i - 1][j][p];
if (p - A[i - 1] >= 0) {
table[i][j][p] += table[i - 1][j - 1][p - A[i - 1]];
}
}
}
}
return table[n][k][target];
}
};
// Time: O(C(n, k))
// Space: O(k)
class Solution_TLE {
public:
/**
* @param A: an integer array.
* @param k: a positive integer (k <= length(A))
* @param target: a integer
* @return an integer
*/
int kSum(vector<int> A, int k, int target) {
int ans = 0;
helper(A, k, 0, target, ans);
return ans;
}
void helper(vector<int> A, int k, int start, int target, int & ans) {
if (k < 0 || target < 0) return;
if (k == 0 && target == 0) {
++ans;
return;
}
for (int i = start; i <= A.size() - k; i++)
helper(A, k - 1, i + 1, target - A[i], ans);
}
};