Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 66e33c7

Browse files
committedApr 5, 2025
Add solution #1155
1 parent 76ac4c3 commit 66e33c7

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,155 LeetCode solutions in JavaScript
1+
# 1,156 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -903,6 +903,7 @@
903903
1146|[Snapshot Array](./solutions/1146-snapshot-array.js)|Medium|
904904
1147|[Longest Chunked Palindrome Decomposition](./solutions/1147-longest-chunked-palindrome-decomposition.js)|Hard|
905905
1154|[Day of the Year](./solutions/1154-day-of-the-year.js)|Easy|
906+
1155|[Number of Dice Rolls With Target Sum](./solutions/1155-number-of-dice-rolls-with-target-sum.js)|Medium|
906907
1161|[Maximum Level Sum of a Binary Tree](./solutions/1161-maximum-level-sum-of-a-binary-tree.js)|Medium|
907908
1189|[Maximum Number of Balloons](./solutions/1189-maximum-number-of-balloons.js)|Easy|
908909
1200|[Minimum Absolute Difference](./solutions/1200-minimum-absolute-difference.js)|Easy|
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 1155. Number of Dice Rolls With Target Sum
3+
* https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/
4+
* Difficulty: Medium
5+
*
6+
* You have n dice, and each dice has k faces numbered from 1 to k.
7+
*
8+
* Given three integers n, k, and target, return the number of possible ways (out of the kn total
9+
* ways) to roll the dice, so the sum of the face-up numbers equals target. Since the answer may
10+
* be too large, return it modulo 109 + 7.
11+
*/
12+
13+
/**
14+
* @param {number} n
15+
* @param {number} k
16+
* @param {number} target
17+
* @return {number}
18+
*/
19+
var numRollsToTarget = function(diceCount, faceCount, targetSum) {
20+
const MOD = 1e9 + 7;
21+
let dp = new Array(targetSum + 1).fill(0);
22+
dp[0] = 1;
23+
24+
for (let dice = 1; dice <= diceCount; dice++) {
25+
const nextDp = new Array(targetSum + 1).fill(0);
26+
for (let sum = 1; sum <= targetSum; sum++) {
27+
for (let face = 1; face <= faceCount && face <= sum; face++) {
28+
nextDp[sum] = (nextDp[sum] + dp[sum - face]) % MOD;
29+
}
30+
}
31+
dp = nextDp;
32+
}
33+
34+
return dp[targetSum];
35+
};

0 commit comments

Comments
 (0)
Please sign in to comment.