LeetCode 0920. Number of Music Playlists Solution in Java, C++, Python & More | Explanation + Code

CoderIndeed
0
0920. Number of Music Playlists

Description

Your music player contains n different songs. You want to listen to goal songs (not necessarily different) during your trip. To avoid boredom, you will create a playlist so that:

  • Every song is played at least once.
  • A song can only be played again only if k other songs have been played.

Given n, goal, and k, return the number of possible playlists that you can create. Since the answer can be very large, return it modulo 109 + 7.

 

Example 1:

Input: n = 3, goal = 3, k = 1
Output: 6
Explanation: There are 6 possible playlists: [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], and [3, 2, 1].

Example 2:

Input: n = 2, goal = 3, k = 0
Output: 6
Explanation: There are 6 possible playlists: [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], and [1, 2, 2].

Example 3:

Input: n = 2, goal = 3, k = 1
Output: 2
Explanation: There are 2 possible playlists: [1, 2, 1] and [2, 1, 2].

 

Constraints:

  • 0 <= k < n <= goal <= 100

Solutions

Solution 1: Dynamic Programming

We define f[i][j] to be the number of playlists that can be made from i songs with exactly j different songs. We have f[0][0] = 1 and the answer is f[goal][n].

For f[i][j], we can choose a song that we have not listened before, so the previous state is f[i - 1][j - 1], and there are n - (j - 1) = n - j + 1 options. Thus, f[i][j] += f[i - 1][j - 1] × (n - j + 1). We can also choose a song that we have listened before, so the previous state is f[i - 1][j], and there are j - k options. Thus, f[i][j] += f[i - 1][j] × (j - k), where j ≥ k.

Therefore, we have the transition equation:

f[i][j] = \begin{cases} 1 & i = 0, j = 0 \ f[i - 1][j - 1] × (n - j + 1) + f[i - 1][j] × (j - k) & i ≥ 1, j ≥ 1 \end{cases}

The final answer is f[goal][n].

The time complexity is O(goal × n), and the space complexity is O(goal × n). Here, goal and n are the parameters given in the problem.

PythonJavaC++GoTypeScriptRust
class Solution: def numMusicPlaylists(self, n: int, goal: int, k: int) -> int: mod = 10**9 + 7 f = [[0] * (n + 1) for _ in range(goal + 1)] f[0][0] = 1 for i in range(1, goal + 1): for j in range(1, n + 1): f[i][j] = f[i - 1][j - 1] * (n - j + 1) if j > k: f[i][j] += f[i - 1][j] * (j - k) f[i][j] %= mod return f[goal][n](code-box)

Solution 2: Dynamic Programming (Space Optimization)

We notice that f[i][j] is only related to f[i - 1][j - 1] and f[i - 1][j]. Therefore, we can use a rolling array to optimize the space complexity, reducing the space complexity to O(n).

PythonJavaC++GoTypeScript
class Solution: def numMusicPlaylists(self, n: int, goal: int, k: int) -> int: mod = 10**9 + 7 f = [0] * (goal + 1) f[0] = 1 for i in range(1, goal + 1): g = [0] * (goal + 1) for j in range(1, n + 1): g[j] = f[j - 1] * (n - j + 1) if j > k: g[j] += f[j] * (j - k) g[j] %= mod f = g return f[n](code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !