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

2 Comments

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

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

@na3744
na3744 Feb 23, 2024 at 2:52 AM

I bought this course, it worth it!

@mk.info.work
mk.info.work Nov 15, 2023 at 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

@sciaku1
sciaku1 Jan 11, 2024 at 3: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 About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support