File tree 1 file changed +38
-0
lines changed
1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int numOfArrays (int n, int m, int k) {
4
+ constexpr int kMod = 1'000'000'007 ;
5
+ // dp[i][j][k] := # of ways to build an array of length i, where j is the
6
+ // Max used num and k is the search_cost
7
+ vector<vector<vector<int >>> dp (
8
+ n + 1 , vector<vector<int >>(m + 1 , vector<int >(k + 1 )));
9
+
10
+ for (int j = 1 ; j <= m; ++j)
11
+ dp[1 ][j][1 ] = 1 ;
12
+
13
+ for (int i = 2 ; i <= n; ++i) // For each length
14
+ for (int j = 1 ; j <= m; ++j) // For each max value
15
+ for (int cost = 1 ; cost <= k; ++cost) { // For each cost
16
+ // 1. appending any of [1, j] in i-th position
17
+ // doesn't change the max and cost
18
+ dp[i][j][cost] = static_cast <long >(j) * dp[i - 1 ][j][cost] % kMod ;
19
+ // 2. appending j in i-th position
20
+ // make j the new max and cost 1
21
+ for (int prevMax = 1 ; prevMax < j; ++prevMax) {
22
+ dp[i][j][cost] += dp[i - 1 ][prevMax][cost - 1 ];
23
+ dp[i][j][cost] %= kMod ;
24
+ }
25
+ }
26
+
27
+ int ans = 0 ;
28
+ for (int j = 1 ; j <= m; ++j) {
29
+ ans += dp[n][j][k];
30
+ ans %= kMod ;
31
+ }
32
+ return ans;
33
+ }
34
+ };
35
+
36
+ // Leetcode POTD
37
+ // CPP
38
+ // 7th Oct
You can’t perform that action at this time.
0 commit comments