Skip to content

Commit 41855b0

Browse files
authored
Merge pull request #328 from cheehwatang/add-1155-NumberOfDiceRollsWithTargetSum
Add 1155. Number of Dice Rolls With Target Sum (Dynamic Programming - Tabulation)
2 parents cffdb17 + 6d2c098 commit 41855b0

File tree

2 files changed

+64
-12
lines changed

2 files changed

+64
-12
lines changed

README.md

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@
2727
<th>Solution</th>
2828
<th>Topics</th>
2929
</tr>
30+
<tr>
31+
<td align="center">September 7th</td>
32+
<td>1155. <a href="https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/">Number of Dice Rolls With Target Sum</a></td>
33+
<td align="center">$\text{\color{Dandelion}Medium}$</td>
34+
<td align="center">
35+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/1155.%20Number%20of%20Dice%20Rolls%20With%20Target%20Sum/NumberOfDiceRollsWithTargetSum_Tabulation.java">Dynamic Programming - Tabulation</a>
36+
</td>
37+
<td align="center">
38+
<a href="#dynamic-programming">Dynamic Programming</a>
39+
</td>
40+
</tr>
3041
<tr>
3142
<td align="center">September 6th</td>
3243
<td>1137. <a href="https://leetcode.com/problems/n-th-tribonacci-number/">N-th Tribonacci Number</a></td>
@@ -77,18 +88,6 @@
7788
<a href="#hash-table">Hash Table</a>
7889
</td>
7990
</tr>
80-
<tr>
81-
<td align="center">September 2nd</td>
82-
<td>923. <a href="https://leetcode.com/problems/3sum-with-multiplicity/">3Sum With Multiplicity</a></td>
83-
<td align="center">$\text{\color{Dandelion}Medium}$</td>
84-
<td align="center">
85-
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/923.%203Sum%20With%20Multiplicity/ThreeSumWithMultiplicity_Counting.java">Counting</a>
86-
</td>
87-
<td align="center">
88-
<a href="#array">Array</a>,
89-
<a href="#counting">Counting</a>
90-
</td>
91-
</tr>
9291
</table>
9392
</br>
9493
<hr>
@@ -4341,6 +4340,18 @@
43414340
</td>
43424341
<td></td>
43434342
</tr>
4343+
<tr>
4344+
<td align="center">1155</td>
4345+
<td><a href="https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/">Number of Dice Rolls With Target Sum</a></td>
4346+
<td align="center">Java with Dynamic Programming using
4347+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/1155.%20Number%20of%20Dice%20Rolls%20With%20Target%20Sum/NumberOfDiceRollsWithTargetSum_Tabulation.java">Tabulation</a>
4348+
</td>
4349+
<td align="center">$\text{\color{Dandelion}Medium}$</td>
4350+
<td align="center">
4351+
<a href="#dynamic-programming">Dynamic Programming</a>
4352+
</td>
4353+
<td></td>
4354+
</tr>
43444355
<tr>
43454356
<td align="center">1578</td>
43464357
<td><a href="https://leetcode.com/problems/minimum-time-to-make-rope-colorful/">Minimum Time to Make Rope Colorful</a></td>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.cheehwatang.leetcode;
2+
3+
// Time Complexity : O(n * k * target),
4+
// where 'n', 'k' and 'target' are the variables used by the method.
5+
// For each dice, 'n', we check each values of 'target' to add the number of faces 'k' that can sum to 'target',
6+
// in a 3 layer nested for-loops.
7+
//
8+
// Space Complexity : O(n * target),
9+
// where 'n' and 'target' are the variables used by the method.
10+
// We use a matrix of 'n' rows with 'target' columns for tabulation.
11+
12+
public class NumberOfDiceRollsWithTargetSum_Tabulation {
13+
14+
// Approach:
15+
// A dynamic programming problem where there are overlapping subproblem,
16+
// for which each roll has possible 1 to k number face-up.
17+
// Here, the iterative and tabulation method is used.
18+
// By reducing the target by the face-up number each roll (hence n - 1),
19+
// the successful sequence of rolls is achieved when n = 0 and target = 0, which forms the base case for the table.
20+
21+
public int numRollsToTarget(int n, int k, int target) {
22+
// As the array is 0-indexed, we need + 1 length to accommodate 'n' and 'target' respectively.
23+
int[][] table = new int[n + 1][target + 1];
24+
// The seed/base case for successful sequence in the table.
25+
table[0][0] = 1;
26+
// Iterate through the table to get the result for all possibilities.
27+
int modulus = (int) (1e9 + 7);
28+
for (int i = 0; i < n; i++) {
29+
for (int j = 0; j < target; j++) {
30+
// Skip if the position in the array is 0, meaning nothing to add to the j + 1 to k positions.
31+
if (table[i][j] == 0) continue;
32+
for (int face = 1; face <= k; face++) {
33+
if (face + j <= target) {
34+
table[i + 1][j + face] = (table[i + 1][j + face] + table[i][j]) % modulus;
35+
}
36+
}
37+
}
38+
}
39+
return table[n][target];
40+
}
41+
}

0 commit comments

Comments
 (0)