> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Number of Music Playlists Python Solution

> Tested Python solution for LeetCode 920 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 920, [Hard](/catalog/hard). Topics: [Math](/catalog/topics/math), [Dynamic Programming](/catalog/topics/dynamic-programming), Combinatorics. [View on LeetCode](https://leetcode.com/problems/number-of-music-playlists/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 920   # by problem number
lcpy gen -s number_of_music_playlists   # by problem name
```

## Problem

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

\<ul>
\<li>Every song is played \<strong>at least once\</strong>.\</li>
\<li>A song can only be played again only if \<code>k\</code> other songs have been played.\</li>
\</ul>

\<p>Given \<code>n\</code>, \<code>goal\</code>, and \<code>k\</code>, return \<em>the number of possible playlists that you can create\</em>. Since the answer can be very large, return it \<strong>modulo\</strong> \<code>10\<sup>9\</sup> + 7\</code>.\</p>

### Examples

```
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].
```

```
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].
```

```
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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_music_playlists/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_music_playlists/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(goal * n)
    # Space: O(n)
    def num_music_playlists(self, n: int, goal: int, k: int) -> int:
        mod = 1_000_000_007
        # dp[j] = number of playlists of current length with j unique songs
        dp = [0] * (n + 1)
        dp[0] = 1
        for _ in range(goal):
            new_dp = [0] * (n + 1)
            for j in range(1, n + 1):
                # Play a new song: choose 1 of the (n - (j - 1)) unused songs
                new_dp[j] = dp[j - 1] * (n - j + 1) % mod
                # Replay a song: any of the j - k previously played songs
                # (a song is replayable once k other songs have been played)
                if j > k:
                    new_dp[j] = (new_dp[j] + dp[j] * (j - k)) % mod
            dp = new_dp
        return dp[n]
```

## Complexity

| Time | Space |
| - | - |
| O(goal \* n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.