[Swift]LeetCode920. 播放列表的數量 | Number of Music Playlists

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-agwzdztc-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip.  You create a playlist so that:git

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

Return the number of possible playlists.  As the answer can be very large, return it modulo 10^9 + 7.github

 Example 1:微信

Input: N = 3, L = 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], [3, 2, 1]. 

Example 2:spa

Input: N = 2, L = 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], [1, 2, 2] 

Example 3:code

Input: N = 2, L = 3, K = 1 Output: 2 Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2] 

 Note:htm

  1. 0 <= K < N <= L <= 100

你的音樂播放器裏有 N 首不一樣的歌,在旅途中,你的旅伴想要聽 L 首歌(不必定不一樣,即,容許歌曲重複)。請你爲她按以下規則建立一個播放列表:blog

  • 每首歌至少播放一次。
  • 一首歌只有在其餘 K 首歌播放完以後才能再次播放。

返回能夠知足要求的播放列表的數量。因爲答案可能很是大,請返回它模 10^9 + 7 的結果。ip

 示例 1:get

輸入:N = 3, L = 3, K = 1
輸出:6
解釋:有 6 種可能的播放列表。[1, 2, 3],[1, 3, 2],[2, 1, 3],[2, 3, 1],[3, 1, 2],[3, 2, 1].

示例 2:

輸入:N = 2, L = 3, K = 0
輸出:6
解釋:有 6 種可能的播放列表。[1, 1, 2],[1, 2, 1],[2, 1, 1],[2, 2, 1],[2, 1, 2],[1, 2, 2]

示例 3:

輸入:N = 2, L = 3, K = 1
輸出:2
解釋:有 2 種可能的播放列表。[1, 2, 1],[2, 1, 2]

 提示:

  1. 0 <= K < N <= L <= 100

56 ms

 1 class Solution {
 2     func numMusicPlaylists(_ N: Int, _ L: Int, _ K: Int) -> Int {
 3         var mod:Int = 1000000007
 4         var dp = Array(repeating:0, count: N+1)
 5         dp[0] = 1
 6         for i in 0..<L
 7         {
 8             var ndp = Array(repeating:0, count: N+1)
 9             for j in 0..<N
10             {
11                 ndp[j + 1] += dp[j] * (N-j)
12                 ndp[j + 1] %= mod
13             }
14             for j in 0...N
15             {
16                 ndp[j] += dp[j] * max(j - K, 0)
17                 ndp[j] %= mod
18             }
19             dp = ndp
20         }
21         return Int(dp[N])
22     }
23 }
相關文章
相關標籤/搜索