-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1155. Number of Dice Rolls With Target Sum.cpp
123 lines (114 loc) · 2.59 KB
/
1155. Number of Dice Rolls With Target Sum.cpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// RECURSION
class Solution {
public:
int help(int n,int k,int i,int target)
{
//base case
if(i>n)
{
if(target==0)
return 1;
else
return 0;
}
if(target==0)
return 1;
//recursive calls
//small calculation
int ans=0;
for(int j=1;j<=k;j++)
{
if(target>=j)
ans+=help(n,k,i+1,target-j);
}
return ans;
}
int numRollsToTarget(int n, int k, int target) {
int ans=help(n,k,1,target);
return ans;
}
};
// MEMOIZATION
#define ll long long
int mod=1e9+7;
class Solution {
public:
ll help(int n,int k,int target,vector<vector<ll>>& memo)
{
//base case
if(n<=0)
{
if(target==0)
return 1;
else
return 0;
}
//memo check
if(memo[n][target]!=-1)
return memo[n][target];
//recursive calls
//small calculation
ll ans=0;
for(int j=1;j<=k;j++)
{
if(target>=j)
ans=(ans+help(n-1,k,target-j,memo))%mod;
}
return memo[n][target]=ans%mod;
}
int numRollsToTarget(int n, int k, int target) {
vector<vector<ll>> memo(n+1,vector<ll>(target+1,-1));
ll ans=help(n,k,target,memo)%mod;
return ans;
}
};
// TABULATION
#define ll long long
int mod=1e9+7;
class Solution {
public:
int numRollsToTarget(int n, int k, int target) {
vector<vector<ll>> dp(n+1,vector<ll>(target+1,0));
dp[0][0]=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=target;j++)
{
int ans=0;
for(int l=1;l<=k;l++)
{
if(j-l>=0)
ans=(ans+dp[i-1][j-l])%mod;
}
dp[i][j]=ans;
}
}
return dp[n][target];
}
};
// SPACE OPTIMIZED
#define ll long long
int mod=1e9+7;
class Solution {
public:
int numRollsToTarget(int n, int k, int target) {
vector<ll> pre(target+1,0);
pre[0]=1;
for(int i=1;i<=n;i++)
{
vector<ll> curr(target+1,0);
for(int j=1;j<=target;j++)
{
int ans=0;
for(int l=1;l<=k;l++)
{
if(j-l>=0)
ans=(ans+pre[j-l])%mod;
}
curr[j]=ans;
}
pre=curr;
}
return pre[target];
}
};