forked from laviii123/Btecky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
squad13
45 lines (37 loc) · 1.06 KB
/
squad13
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
class Solution {
public:
vector<vector<vector<int>>> memo;
int MOD = 1e9 + 7;
int n;
int m;
int numOfArrays(int n, int m, int k) {
memo = vector(n, vector(m + 1, vector(k + 1, -1)));
this->n = n;
this->m = m;
return dp(0, 0, k);
}
int dp(int i, int maxSoFar, int remain) {
if (i == n) {
if (remain == 0) {
return 1;
}
return 0;
}
if (remain < 0) {
return 0;
}
if (memo[i][maxSoFar][remain] != -1) {
return memo[i][maxSoFar][remain];
}
int ans = 0;
for (int num = 1; num <= maxSoFar; num++) {
ans = (ans + dp(i + 1, maxSoFar, remain)) % MOD;
}
for (int num = maxSoFar + 1; num <= m; num++) {
ans = (ans + dp(i + 1, num, remain - 1)) % MOD;
}
memo[i][maxSoFar][remain] = ans;
return ans;
}
};
// @gfgleet Leetcode POTD