-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
39 lines (31 loc) · 934 Bytes
/
Copy pathsolution.cpp
File metadata and controls
39 lines (31 loc) · 934 Bytes
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
class Solution
{
public:
int noOfWays(int m, int n, int x)
{
// dp[j] will store number of ways to get sum j
vector<long long> dp(x + 1, 0);
// Base case: 1 way to get sum 0 using 0 dice
dp[0] = 1;
// Iterate for each dice
for (int dice = 1; dice <= n; dice++)
{
// Temporary array for current dice calculations
vector<long long> temp(x + 1, 0);
for (int sum = 1; sum <= x; sum++)
{
// Try every possible face value
for (int face = 1; face <= m; face++)
{
if (sum - face >= 0)
{
temp[sum] += dp[sum - face];
}
}
}
// Move temp results to dp
dp = temp;
}
return dp[x];
}
};