- Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path0920-number-of-music-playlists.rb
60 lines (50 loc) · 1.71 KB
/
0920-number-of-music-playlists.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# frozen_string_literal: true
# 920. Number of Music Playlists
# Hard
# https://leetcode.com/problems/number-of-music-playlists
=begin
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
=end
# @param {Integer} n
# @param {Integer} goal
# @param {Integer} k
# @return {Integer}
defnum_music_playlists(n,goal,k)
mod=10**9 + 7
dp=Array.new(goal + 1){Array.new(n + 1,0)}
dp[0][0]=1
foriin1..goal
forjin1..n
dp[i][j]=(dp[i - 1][j - 1] * (n - j + 1) + dp[i - 1][j] * [j - k,0].max) % mod
end
end
dp[goal][n]
end
# **************** #
# TEST #
# **************** #
require"test/unit"
classTest_num_music_playlists < Test::Unit::TestCase
deftest_
assert_equal6,num_music_playlists(3,3,1)
assert_equal6,num_music_playlists(2,3,0)
assert_equal2,num_music_playlists(2,3,1)
end
end