File tree Expand file tree Collapse file tree 4 files changed +61
-9
lines changed
solution/1100-1199/1155.Number of Dice Rolls With Target Sum Expand file tree Collapse file tree 4 files changed +61
-9
lines changed Original file line number Diff line number Diff line change @@ -91,15 +91,14 @@ class Solution:
91
91
92
92
``` java
93
93
class Solution {
94
- private static final int MOD = (int ) 1e9 + 7 ;
95
-
96
94
public int numRollsToTarget (int n , int k , int target ) {
95
+ final int mod = (int ) 1e9 + 7 ;
97
96
int [][] f = new int [n + 1 ][target + 1 ];
98
97
f[0 ][0 ] = 1 ;
99
98
for (int i = 1 ; i <= n; ++ i) {
100
99
for (int j = 1 ; j <= Math . min(target, i * k); ++ j) {
101
100
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 ;
103
102
}
104
103
}
105
104
}
@@ -158,6 +157,26 @@ func min(a, b int) int {
158
157
}
159
158
```
160
159
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
+
161
180
### ** ...**
162
181
163
182
```
Original file line number Diff line number Diff line change @@ -66,15 +66,14 @@ class Solution:
66
66
67
67
``` java
68
68
class Solution {
69
- private static final int MOD = (int ) 1e9 + 7 ;
70
-
71
69
public int numRollsToTarget (int n , int k , int target ) {
70
+ final int mod = (int ) 1e9 + 7 ;
72
71
int [][] f = new int [n + 1 ][target + 1 ];
73
72
f[0 ][0 ] = 1 ;
74
73
for (int i = 1 ; i <= n; ++ i) {
75
74
for (int j = 1 ; j <= Math . min(target, i * k); ++ j) {
76
75
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 ;
78
77
}
79
78
}
80
79
}
@@ -133,6 +132,26 @@ func min(a, b int) int {
133
132
}
134
133
```
135
134
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
+
136
155
### ** ...**
137
156
138
157
```
Original file line number Diff line number Diff line change 1
1
class Solution {
2
- private static final int MOD = (int ) 1e9 + 7 ;
3
-
4
2
public int numRollsToTarget (int n , int k , int target ) {
3
+ final int mod = (int ) 1e9 + 7 ;
5
4
int [][] f = new int [n + 1 ][target + 1 ];
6
5
f [0 ][0 ] = 1 ;
7
6
for (int i = 1 ; i <= n ; ++i ) {
8
7
for (int j = 1 ; j <= Math .min (target , i * k ); ++j ) {
9
8
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 ;
11
10
}
12
11
}
13
12
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments