Skip to content

Commit 36b29de

Browse files
authored
Create number-of-dice-rolls-with-target-sum.cpp
1 parent 83af70c commit 36b29de

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Time: O(d * f * t)
2+
// Space: O(t)
3+
4+
class Solution {
5+
public:
6+
int numRollsToTarget(int d, int f, int target) {
7+
static const int MOD = 1e9 + 7;
8+
vector<vector<int>> dp(2, vector<int>(target + 1));
9+
dp[0][0] = 1;
10+
for (int i = 1; i <= d; ++i) {
11+
dp[i % 2] = vector<int>(target + 1);
12+
for (int k = 1; k <= f; ++k) {
13+
for (int j = k; j <= target; ++j) {
14+
dp[i % 2][j] = (dp[i % 2][j] + dp[(i - 1) % 2][j - k]) % MOD;
15+
}
16+
}
17+
}
18+
return dp[d % 2][target] % MOD;
19+
}
20+
};

0 commit comments

Comments
 (0)