Number Of Dice Roll With Target Sum

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 106:- Number Of Dice Roll With Target Sum

The "Number of Dice Rolls With Target Sum" problem is a classic dynamic programming problem. Given the number of dice d, the number of faces on each die f, and a target sum target, you need to find the number of ways to roll the dice to get the target sum.

Here's a Python function that implements the Number of Dice Rolls With Target Sum algorithm using dynamic programming:

pythonCopy code

def num_dice_rolls(d, f, target):    MOD = 10**9 + 7    dp = [[0] * (target + 1) for _ in range(d + 1)]    dp[0][0] = 1    for i in range(1, d + 1):        for j in range(1, target + 1):            for k in range(1, min(f, j) + 1):                dp[i][j] = (dp[i][j] + dp[i - 1][j - k]) % MOD    return dp[d][target] # Test the function print(num_dice_rolls(2, 6, 7))   # Output: 6 (Possible combinations: [1, 6], [2, 5], [3, 4], [4, 3], [5, 2], [6, 1]) print(num_dice_rolls(2, 6, 12))  # Output: 1 (Only one combination: [6, 6]) print(num_dice_rolls(3, 4, 5))   # Output: 6 (Possible combinations: [1, 1, 3], [1, 2, 2], [1, 3, 1], [2, 1, 2], [2, 2, 1], [3, 1, 1])

In this code, the num_dice_rolls() function takes the number of dice d, the number of faces on each die f, and the target sum target as input.

The dynamic programming approach uses a 2D array dp to store the number of ways to roll the dice to get each possible sum from 0 to the target. The entry dp[i][j] represents the number of ways to get the sum j using i dice.

The algorithm iteratively fills the dp array based on the recurrence relation. For each additional die, it calculates the number of ways to get each possible sum by considering the possible outcomes of rolling the current die.

The result is stored in dp[d][target], which represents the number of ways to roll the dice to get the target sum. The time complexity of this solution is O(d * f * target), where d is the number of dice, f is the number of faces on each die, and target is the target sum. The space complexity is also O(d * target) due to the dynamic programming array dp.

14. Week7 - Assignments

Comments: 2

profile
@mk.info.work
17-Feb-2024, 10:20 PM

SCIAKU Team please upload 1st video of TREE please please please, please

profile
@na3744
23-Feb-2024, 02:52 AM

I bought this course, it worth it!

profile
@mk.info.work
15-Nov-2023, 10:25 PM

Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it

profile
@sciaku1
11-Jan-2024, 03:23 PM

Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?