Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3fbfd5f

Browse files
Merge pull request #329 from cheehwatang/add-1155-NumberOfDiceRollsWithTargetSum
Add 1155. Number of Dice Rolls With Target Sum (Dynamic Programming - Memoization)
2 parents 41855b0 + ee1e11c commit 3fbfd5f

File tree

2 files changed

+64
-12
lines changed

2 files changed

+64
-12
lines changed

‎README.md‎

Lines changed: 12 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 8th</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_Memoization.java">Dynamic Programming - Memoization</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 7th</td>
3243
<td>1155. <a href="https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/">Number of Dice Rolls With Target Sum</a></td>
@@ -76,18 +87,6 @@
7687
<a href="#two-pointers">Two Pointers</a>
7788
</td>
7889
</tr>
79-
<tr>
80-
<td align="center">September 3rd</td>
81-
<td>923. <a href="https://leetcode.com/problems/3sum-with-multiplicity/">3Sum With Multiplicity</a></td>
82-
<td align="center">$\text{\color{Dandelion}Medium}$</td>
83-
<td align="center">
84-
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/923.%203Sum%20With%20Multiplicity/ThreeSumWithMultiplicity_HashTable.java">Hash Table</a>
85-
</td>
86-
<td align="center">
87-
<a href="#array">Array</a>,
88-
<a href="#hash-table">Hash Table</a>
89-
</td>
90-
</tr>
9190
</table>
9291
</br>
9392
<hr>
@@ -4344,6 +4343,7 @@
43444343
<td align="center">1155</td>
43454344
<td><a href="https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/">Number of Dice Rolls With Target Sum</a></td>
43464345
<td align="center">Java with Dynamic Programming using
4346+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/1155.%20Number%20of%20Dice%20Rolls%20With%20Target%20Sum/NumberOfDiceRollsWithTargetSum_Memoization.java">Memoization</a> or
43474347
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/1155.%20Number%20of%20Dice%20Rolls%20With%20Target%20Sum/NumberOfDiceRollsWithTargetSum_Tabulation.java">Tabulation</a>
43484348
</td>
43494349
<td align="center">$\text{\color{Dandelion}Medium}$</td>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.cheehwatang.leetcode;
2+
3+
import java.util.Arrays;
4+
5+
// Time Complexity : O(n * k),
6+
// where 'n' and 'k' are the variables used by the method.
7+
// For every face, we check every dice, to find all the possible combinations that sums to 'target',
8+
// thus we can treat it like nested for-loop.
9+
//
10+
// Space Complexity : O(n * target),
11+
// where 'n' and 'target' are the variables used by the method.
12+
// We use a matrix of 'n' rows with 'target' columns for memoization.
13+
// The recursive call stack has a maximum height of 'n'.
14+
15+
public class NumberOfDiceRollsWithTargetSum_Memoization {
16+
17+
// Approach:
18+
// A dynamic programming problem where there are overlapping subproblem,
19+
// for which each roll has possible 1 to k number face-up.
20+
// Here, the recursive and memoization method is used.
21+
// By reducing the target by the face-up number each roll (hence n - 1),
22+
// the successful sequence of rolls is achieved when n = 0 and target = 0,
23+
// which forms the base case where target == 0 && n == 0.
24+
// If target <= 0 || n == 0, meaning the sequence is unsuccessful.
25+
26+
// Wrapper method to initiate the memo and call the recursive method.
27+
// As the number 0 in the memo is a meaningful result, set all the elements of the memo to -1.
28+
public int numRollsToTarget(int n, int k, int target) {
29+
// As the array is 0-indexed, we need + 1 length to accommodate 'n' and 'target' respectively.
30+
int[][] memo = new int[n + 1][target + 1];
31+
for (int[] row : memo) Arrays.fill(row, -1);
32+
return numRollsToTarget(n, k, target, memo);
33+
}
34+
35+
// Recursive method.
36+
private int numRollsToTarget(int n, int k, int target, int[][] memo) {
37+
// Base case for successful sequence.
38+
if (target == 0 && n == 0) return 1;
39+
// Base case for unsuccessful sequence.
40+
if (target <= 0 || n == 0) return 0;
41+
// If memo already contains the result, return the result.
42+
if (memo[n][target] != -1) return memo[n][target];
43+
int modulus = (int) (1e9 + 7);
44+
int count = 0;
45+
// For each face, reduce the 'n' by 1 and 'target' by the face-up number.
46+
for (int face = 1; face <= k; face++) {
47+
count = (count + numRollsToTarget(n - 1, k, target - face, memo)) % modulus;
48+
}
49+
// Store the result into the memo and return the result.
50+
return memo[n][target] = count;
51+
}
52+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /