Skip to content

Commit 4c365e8

Browse files
committed
feat: add solutions to lc problem: No.1155
No.1155. Number of Dice Rolls With Target Sum
1 parent 22823be commit 4c365e8

File tree

4 files changed

+61
-9
lines changed

4 files changed

+61
-9
lines changed

solution/1100-1199/1155.Number of Dice Rolls With Target Sum/README.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,14 @@ class Solution:
9191

9292
```java
9393
class Solution {
94-
private static final int MOD = (int) 1e9 + 7;
95-
9694
public int numRollsToTarget(int n, int k, int target) {
95+
final int mod = (int) 1e9 + 7;
9796
int[][] f = new int[n + 1][target + 1];
9897
f[0][0] = 1;
9998
for (int i = 1; i <= n; ++i) {
10099
for (int j = 1; j <= Math.min(target, i * k); ++j) {
101100
for (int h = 1; h <= Math.min(j, k); ++h) {
102-
f[i][j] = (f[i][j] + f[i - 1][j - h]) % MOD;
101+
f[i][j] = (f[i][j] + f[i - 1][j - h]) % mod;
103102
}
104103
}
105104
}
@@ -158,6 +157,26 @@ func min(a, b int) int {
158157
}
159158
```
160159

160+
### **TypeScript**
161+
162+
```ts
163+
function numRollsToTarget(n: number, k: number, target: number): number {
164+
const f = Array(n + 1)
165+
.fill(0)
166+
.map(() => Array(target + 1).fill(0));
167+
f[0][0] = 1;
168+
const mod = 1e9 + 7;
169+
for (let i = 1; i <= n; ++i) {
170+
for (let j = 1; j <= Math.min(i * k, target); ++j) {
171+
for (let h = 1; h <= Math.min(j, k); ++h) {
172+
f[i][j] = (f[i][j] + f[i - 1][j - h]) % mod;
173+
}
174+
}
175+
}
176+
return f[n][target];
177+
}
178+
```
179+
161180
### **...**
162181

163182
```

solution/1100-1199/1155.Number of Dice Rolls With Target Sum/README_EN.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,14 @@ class Solution:
6666

6767
```java
6868
class Solution {
69-
private static final int MOD = (int) 1e9 + 7;
70-
7169
public int numRollsToTarget(int n, int k, int target) {
70+
final int mod = (int) 1e9 + 7;
7271
int[][] f = new int[n + 1][target + 1];
7372
f[0][0] = 1;
7473
for (int i = 1; i <= n; ++i) {
7574
for (int j = 1; j <= Math.min(target, i * k); ++j) {
7675
for (int h = 1; h <= Math.min(j, k); ++h) {
77-
f[i][j] = (f[i][j] + f[i - 1][j - h]) % MOD;
76+
f[i][j] = (f[i][j] + f[i - 1][j - h]) % mod;
7877
}
7978
}
8079
}
@@ -133,6 +132,26 @@ func min(a, b int) int {
133132
}
134133
```
135134

135+
### **TypeScript**
136+
137+
```ts
138+
function numRollsToTarget(n: number, k: number, target: number): number {
139+
const f = Array(n + 1)
140+
.fill(0)
141+
.map(() => Array(target + 1).fill(0));
142+
f[0][0] = 1;
143+
const mod = 1e9 + 7;
144+
for (let i = 1; i <= n; ++i) {
145+
for (let j = 1; j <= Math.min(i * k, target); ++j) {
146+
for (let h = 1; h <= Math.min(j, k); ++h) {
147+
f[i][j] = (f[i][j] + f[i - 1][j - h]) % mod;
148+
}
149+
}
150+
}
151+
return f[n][target];
152+
}
153+
```
154+
136155
### **...**
137156

138157
```

solution/1100-1199/1155.Number of Dice Rolls With Target Sum/Solution.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
class Solution {
2-
private static final int MOD = (int) 1e9 + 7;
3-
42
public int numRollsToTarget(int n, int k, int target) {
3+
final int mod = (int) 1e9 + 7;
54
int[][] f = new int[n + 1][target + 1];
65
f[0][0] = 1;
76
for (int i = 1; i <= n; ++i) {
87
for (int j = 1; j <= Math.min(target, i * k); ++j) {
98
for (int h = 1; h <= Math.min(j, k); ++h) {
10-
f[i][j] = (f[i][j] + f[i - 1][j - h]) % MOD;
9+
f[i][j] = (f[i][j] + f[i - 1][j - h]) % mod;
1110
}
1211
}
1312
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function numRollsToTarget(n: number, k: number, target: number): number {
2+
const f = Array(n + 1)
3+
.fill(0)
4+
.map(() => Array(target + 1).fill(0));
5+
f[0][0] = 1;
6+
const mod = 1e9 + 7;
7+
for (let i = 1; i <= n; ++i) {
8+
for (let j = 1; j <= Math.min(i * k, target); ++j) {
9+
for (let h = 1; h <= Math.min(j, k); ++h) {
10+
f[i][j] = (f[i][j] + f[i - 1][j - h]) % mod;
11+
}
12+
}
13+
}
14+
return f[n][target];
15+
}

0 commit comments

Comments
 (0)