2 parents 8b181a8 + 82a04b6 commit 4ca6256Copy full SHA for 4ca6256
1 file changed
algorithms/dynamic_programming/dice.py
@@ -0,0 +1,24 @@
1
+'''
2
+Given n dices each with m faces, numbered from 1 to m,
3
+find the number of ways to get a given sum X.
4
+X is the summation of values on each face when all the dice are thrown.
5
+
6
7
8
+dp = [[-1 for i in range(x+1)] for j in range(n+1)]
9
10
11
+def num_of_ways(m, n, x):
12
+ if x == 0 and n == 0:
13
+ return 1
14
+ if x < 0 or n == 0:
15
+ return 0
16
+ if dp[n][x] != -1:
17
+ return dp[n][x]
18
19
+ ans = 0
20
+ for i in range(1, m + 1):
21
+ ans += num_of_ways(m, n - 1, x - i)
22
23
+ dp[n][x] = ans
24
+ return ans
0 commit comments