diff --git a/README.md b/README.md index b0e3967..c0d0852 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,17 @@ Solution Topics + + September 7th + 1155. Number of Dice Rolls With Target Sum + $\text{\color{Dandelion}Medium}$ + + Dynamic Programming - Tabulation + + + Dynamic Programming + + September 6th 1137. N-th Tribonacci Number @@ -77,18 +88,6 @@ Hash Table - - September 2nd - 923. 3Sum With Multiplicity - $\text{\color{Dandelion}Medium}$ - - Counting - - - Array, - Counting - -

@@ -4341,6 +4340,18 @@ + + 1155 + Number of Dice Rolls With Target Sum + Java with Dynamic Programming using + Tabulation + + $\text{\color{Dandelion}Medium}$ + + Dynamic Programming + + + 1578 Minimum Time to Make Rope Colorful diff --git a/solutions/1155. Number of Dice Rolls With Target Sum/NumberOfDiceRollsWithTargetSum_Tabulation.java b/solutions/1155. Number of Dice Rolls With Target Sum/NumberOfDiceRollsWithTargetSum_Tabulation.java new file mode 100644 index 0000000..ce0dfbe --- /dev/null +++ b/solutions/1155. Number of Dice Rolls With Target Sum/NumberOfDiceRollsWithTargetSum_Tabulation.java @@ -0,0 +1,41 @@ +package com.cheehwatang.leetcode; + +// Time Complexity : O(n * k * target), +// where 'n', 'k' and 'target' are the variables used by the method. +// For each dice, 'n', we check each values of 'target' to add the number of faces 'k' that can sum to 'target', +// in a 3 layer nested for-loops. +// +// Space Complexity : O(n * target), +// where 'n' and 'target' are the variables used by the method. +// We use a matrix of 'n' rows with 'target' columns for tabulation. + +public class NumberOfDiceRollsWithTargetSum_Tabulation { + + // Approach: + // A dynamic programming problem where there are overlapping subproblem, + // for which each roll has possible 1 to k number face-up. + // Here, the iterative and tabulation method is used. + // By reducing the target by the face-up number each roll (hence n - 1), + // the successful sequence of rolls is achieved when n = 0 and target = 0, which forms the base case for the table. + + public int numRollsToTarget(int n, int k, int target) { + // As the array is 0-indexed, we need + 1 length to accommodate 'n' and 'target' respectively. + int[][] table = new int[n + 1][target + 1]; + // The seed/base case for successful sequence in the table. + table[0][0] = 1; + // Iterate through the table to get the result for all possibilities. + int modulus = (int) (1e9 + 7); + for (int i = 0; i < n; i++) { + for (int j = 0; j < target; j++) { + // Skip if the position in the array is 0, meaning nothing to add to the j + 1 to k positions. + if (table[i][j] == 0) continue; + for (int face = 1; face <= k; face++) { + if (face + j <= target) { + table[i + 1][j + face] = (table[i + 1][j + face] + table[i][j]) % modulus; + } + } + } + } + return table[n][target]; + } +}