Skip to content

Commit 2263d6e

Browse files
committed
Add solution #920
1 parent e59ed9c commit 2263d6e

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,7 @@
729729
916|[Word Subsets](./solutions/0916-word-subsets.js)|Medium|
730730
918|[Maximum Sum Circular Subarray](./solutions/0918-maximum-sum-circular-subarray.js)|Medium|
731731
919|[Complete Binary Tree Inserter](./solutions/0919-complete-binary-tree-inserter.js)|Medium|
732+
920|[Number of Music Playlists](./solutions/0920-number-of-music-playlists.js)|Hard|
732733
922|[Sort Array By Parity II](./solutions/0922-sort-array-by-parity-ii.js)|Easy|
733734
925|[Long Pressed Name](./solutions/0925-long-pressed-name.js)|Easy|
734735
926|[Flip String to Monotone Increasing](./solutions/0926-flip-string-to-monotone-increasing.js)|Medium|
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* 920. Number of Music Playlists
3+
* https://leetcode.com/problems/number-of-music-playlists/
4+
* Difficulty: Hard
5+
*
6+
* Your music player contains n different songs. You want to listen to goal songs (not necessarily
7+
* different) during your trip. To avoid boredom, you will create a playlist so that:
8+
* - Every song is played at least once.
9+
* - A song can only be played again only if k other songs have been played.
10+
*
11+
* Given n, goal, and k, return the number of possible playlists that you can create. Since the
12+
* answer can be very large, return it modulo 109 + 7.
13+
*/
14+
15+
/**
16+
* @param {number} n
17+
* @param {number} goal
18+
* @param {number} k
19+
* @return {number}
20+
*/
21+
var numMusicPlaylists = function(n, goal, k) {
22+
const MOD = 1e9 + 7;
23+
const dp = new Array(goal + 1).fill().map(() => new Array(n + 1).fill(0));
24+
dp[0][0] = 1;
25+
26+
for (let songs = 1; songs <= goal; songs++) {
27+
for (let uniques = 1; uniques <= Math.min(songs, n); uniques++) {
28+
dp[songs][uniques] = dp[songs - 1][uniques - 1] * (n - (uniques - 1));
29+
if (uniques > k) {
30+
dp[songs][uniques] += dp[songs - 1][uniques] * (uniques - k);
31+
}
32+
dp[songs][uniques] %= MOD;
33+
}
34+
}
35+
36+
return dp[goal][n];
37+
};

0 commit comments

Comments
 (0)