File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Time: O(m * n), m is the max of rollMax
2
+ // Space: O(m)
3
+
4
+ class Solution {
5
+ public:
6
+ int dieSimulator (int n, vector<int >& rollMax) {
7
+ static const int MOD = 1e9 + 7 ;
8
+ vector<vector<int >> dp;
9
+ for (int i = 0 ; i < 6 ; ++i) {
10
+ dp.emplace_back (rollMax[i]);
11
+ dp[i][0 ] = 1 ; // 0-indexed
12
+ }
13
+ while (--n) {
14
+ vector<vector<int >> new_dp;
15
+ for (int i = 0 ; i < 6 ; ++i) {
16
+ new_dp.emplace_back (rollMax[i]);
17
+ }
18
+ for (int i = 0 ; i < 6 ; ++i) {
19
+ for (int k = 0 ; k < rollMax[i]; ++k) {
20
+ for (int j = 0 ; j < 6 ; ++j) {
21
+ if (i == j) {
22
+ if (k < rollMax[i] - 1 ) { // 0-indexed
23
+ new_dp[j][k + 1 ] = (new_dp[j][k + 1 ] + dp[i][k]) % MOD;
24
+ }
25
+ } else {
26
+ new_dp[j][0 ] = (new_dp[j][0 ] + dp[i][k]) % MOD;
27
+ }
28
+ }
29
+ }
30
+ }
31
+ dp = move (new_dp);
32
+ }
33
+ uint64_t result = 0 ;
34
+ for (const auto & row : dp) {
35
+ const auto & total =
36
+ accumulate (row.cbegin (), row.cend (),
37
+ 0ull ,
38
+ [&](const auto & a, const auto & b) {
39
+ return (a + b) % MOD;
40
+ });
41
+ result = (result + total) % MOD;
42
+ }
43
+ return result;
44
+ }
45
+ };
You can’t perform that action at this time.
0 commit comments