반응형
https://leetcode.com/problems/number-of-dice-rolls-with-target-sum
각 횟수에서의 sum을 저장해줍니다.
dp[n][target] = dp[n-1][target - (1...k)]
class Solution {
public:
int dp[31][1001];
int numRollsToTarget(int n, int k, int target) {
memset(dp, -1, sizeof(dp));
return f(n, k, target);
}
int f(int n, int k, int target) {
if(n == 0) {
return target == 0;
}
if(target < 0) return 0;
int& c = dp[n][target];
if (c != -1) return c;
c = 0;
for(int i=1; i<=k; i++) {
c += f(n - 1, k, target - i);
c %= 1000000007;
}
return c;
}
};
반응형
'Algorithm' 카테고리의 다른 글
LeetCode 1897. Redistribute Characters to Make All Strings Equal (0) | 2023.12.30 |
---|---|
LeetCode 1335. Minimum Difficulty of a Job Schedule (0) | 2023.12.29 |
LeetCode 91. Decode Ways (0) | 2023.12.25 |
LeetCode 1758. Minimum Changes To Make Alternating Binary String (0) | 2023.12.25 |
LeetCode 1496. Path Crossing (1) | 2023.12.23 |