File tree 3 files changed +97
-0
lines changed
solution/0900-0999/0920.Number of Music Playlists
3 files changed +97
-0
lines changed Original file line number Diff line number Diff line change @@ -204,6 +204,40 @@ public:
204
204
};
205
205
```
206
206
207
+ ### ** Rust**
208
+
209
+ ``` rust
210
+ impl Solution {
211
+ #[allow(dead_code)]
212
+ pub fn num_music_playlists (n : i32 , goal : i32 , k : i32 ) -> i32 {
213
+ let mut dp : Vec <Vec <i64 >> = vec! [vec! [0 ; n as usize + 1 ]; goal as usize + 1 ];
214
+
215
+ // Initialize the dp vector
216
+ dp [0 ][0 ] = 1 ;
217
+
218
+ // Begin the dp process
219
+ for i in 1 ..= goal as usize {
220
+ for j in 1 ..= n as usize {
221
+ // Choose the song that has not been chosen before
222
+ // We have n - (j - 1) songs to choose
223
+ dp [i ][j ] += dp [i - 1 ][j - 1 ] * ((n - (j as i32 - 1 ))) as i64 ;
224
+
225
+ // Choose the song that has been chosen before
226
+ // We have j - k songs to choose if j > k
227
+ if j as i32 > k {
228
+ dp [i ][j ] += dp [i - 1 ][j ] * (j as i32 - k ) as i64 ;
229
+ }
230
+
231
+ // Update dp[i][j]
232
+ dp [i ][j ] %= (1e 9 as i32 + 7 ) as i64 ;
233
+ }
234
+ }
235
+
236
+ dp [goal as usize ][n as usize ] as i32
237
+ }
238
+ }
239
+ ```
240
+
207
241
### ** Go**
208
242
209
243
``` go
Original file line number Diff line number Diff line change @@ -194,6 +194,40 @@ public:
194
194
};
195
195
```
196
196
197
+ ### ** Rust**
198
+
199
+ ``` rust
200
+ impl Solution {
201
+ #[allow(dead_code)]
202
+ pub fn num_music_playlists (n : i32 , goal : i32 , k : i32 ) -> i32 {
203
+ let mut dp : Vec <Vec <i64 >> = vec! [vec! [0 ; n as usize + 1 ]; goal as usize + 1 ];
204
+
205
+ // Initialize the dp vector
206
+ dp [0 ][0 ] = 1 ;
207
+
208
+ // Begin the dp process
209
+ for i in 1 ..= goal as usize {
210
+ for j in 1 ..= n as usize {
211
+ // Choose the song that has not been chosen before
212
+ // We have n - (j - 1) songs to choose
213
+ dp [i ][j ] += dp [i - 1 ][j - 1 ] * ((n - (j as i32 - 1 ))) as i64 ;
214
+
215
+ // Choose the song that has been chosen before
216
+ // We have j - k songs to choose if j > k
217
+ if j as i32 > k {
218
+ dp [i ][j ] += dp [i - 1 ][j ] * (j as i32 - k ) as i64 ;
219
+ }
220
+
221
+ // Update dp[i][j]
222
+ dp [i ][j ] %= (1e 9 as i32 + 7 ) as i64 ;
223
+ }
224
+ }
225
+
226
+ dp [goal as usize ][n as usize ] as i32
227
+ }
228
+ }
229
+ ```
230
+
197
231
### ** Go**
198
232
199
233
``` go
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ #[ allow( dead_code) ]
3
+ pub fn num_music_playlists ( n : i32 , goal : i32 , k : i32 ) -> i32 {
4
+ let mut dp: Vec < Vec < i64 > > = vec ! [ vec![ 0 ; n as usize + 1 ] ; goal as usize + 1 ] ;
5
+
6
+ // Initialize the dp vector
7
+ dp[ 0 ] [ 0 ] = 1 ;
8
+
9
+ // Begin the dp process
10
+ for i in 1 ..=goal as usize {
11
+ for j in 1 ..=n as usize {
12
+ // Choose the song that has not been chosen before
13
+ // We have n - (j - 1) songs to choose
14
+ dp[ i] [ j] += dp[ i - 1 ] [ j - 1 ] * ( ( n - ( j as i32 - 1 ) ) ) as i64 ;
15
+
16
+ // Choose the song that has been chosen before
17
+ // We have j - k songs to choose if j > k
18
+ if j as i32 > k {
19
+ dp[ i] [ j] += dp[ i - 1 ] [ j] * ( j as i32 - k) as i64 ;
20
+ }
21
+
22
+ // Update dp[i][j]
23
+ dp[ i] [ j] %= ( 1e9 as i32 + 7 ) as i64 ;
24
+ }
25
+ }
26
+
27
+ dp[ goal as usize ] [ n as usize ] as i32
28
+ }
29
+ }
You can’t perform that action at this time.
0 commit comments