868. Playlist Builder

A radio host owns n different songs and must fill a programme of exactly goal slots in order. The programme must obey two rules: every song is played at least once, and after a song is played it cannot be played again until k other songs have been played in between (so two plays of the same song are at least k + 1 slots apart; k = 0 allows repeats freely).

Return the number of different programmes, modulo 1_000_000_007 (10^9 + 7). Two programmes differ if some slot plays a different song.

Example 1

Input:
n = 3, goal = 4, k = 1
Output:
18
Explanation:

Four plays use three songs, so one song is played twice with at least one other song in between: 3 choices of song x 6 valid arrangements = 18.

Example 2

Input:
n = 2, goal = 3, k = 0
Output:
6
Explanation:

With no cooldown, any length-3 sequence of the two songs is allowed except the two that use a single song: 8 - 2 = 6.

Example 3

Input:
n = 3, goal = 3, k = 2
Output:
6
Explanation:

Three plays of three songs with a cooldown of two forces all songs different, which is 3! = 6 orders.

Constraints

  • 0 ≤ k < n ≤ goal ≤ 300

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms

Expected complexity

Time
O(goal * n)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…