Dice Throw
Given n dice each with m faces, numbered from 1 to m, find the number of ways to get sum X. X is the summation of values on each face when all the dice are thrown.
The Naive approach is to find all the possible combinations of values from n dice and keep on counting the results that sum to X.
This problem can be efficiently solved using Dynamic Programming (DP).
Why DP approach? The above problem exhibits overlapping subproblems. See the below diagram.
Let there be 3 dice, each with 6 faces and we need to find the number of ways to get sum 8:
Please take a closer look at the above recursion.
The sub-problems in RED are solved first time and sub-problems in BLUE are solved again (exhibit overlapping sub-problems). Hence, storing the results of the solved sub-problems saves time.
Last updated