mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-26 05:59:22 +08:00
@ -15,11 +15,12 @@ Following is implementation of Dynamic Programming approach. */
|
||||
// dice where every dice has 'm' faces
|
||||
class DP {
|
||||
|
||||
/* The main function that returns the number of ways to get sum 'x' with 'n' dice and 'm' with m faces. */
|
||||
/* The main function that returns the number of ways to get sum 'x' with 'n' dice and 'm' with m
|
||||
* faces. */
|
||||
public static long findWays(int m, int n, int x) {
|
||||
/* Create a table to store the results of subproblems.
|
||||
One extra row and column are used for simplicity
|
||||
(Number of dice is directly used as row index and sum is directly used as column index).
|
||||
/* Create a table to store the results of subproblems.
|
||||
One extra row and column are used for simplicity
|
||||
(Number of dice is directly used as row index and sum is directly used as column index).
|
||||
The entries in 0th row and 0th column are never used. */
|
||||
long[][] table = new long[n + 1][x + 1];
|
||||
|
||||
@ -28,7 +29,7 @@ class DP {
|
||||
table[1][j] = 1;
|
||||
}
|
||||
|
||||
/* Fill rest of the entries in table using recursive relation
|
||||
/* Fill rest of the entries in table using recursive relation
|
||||
i: number of dice, j: sum */
|
||||
for (int i = 2; i <= n; i++) {
|
||||
for (int j = 1; j <= x; j++) {
|
||||
|
Reference in New Issue
Block a user