链接:https://leetcode-cn.com/problems/number-of-dice-rolls-with-target-sum/
dp数组用于记录使用
个骰子掷出
的方法数,下一层只需在上一层的基础上累加。
java代码:
class Solution {public int numRollsToTarget(int d, int f, int target) {int dp[][] = new int [d+1][target+1];for(int i = 1;i<=f&&i<=target;i++)dp[1][i] = 1;for(int i = 2;i<=d;i++){for(int j = i;j<=target;j++){if(j>i*f)break;for(int k = 1;k<=f;k++){if(k>j)break;dp[i][j] += dp[i-1][j-k];dp[i][j]%=1000000007;}}}return dp[d][target];}
}