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:

  • 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.

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:

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:

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

Note:

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

Approach #1: DP. [C++]

class Solution {
public int numMusicPlaylists(int N, int L, int K) {
int mod = (int)Math.pow(10, 9) + 7;
long[][] dp = new long[L+1][N+1];
dp[0][0] = 1;
for (int i = 1; i <= L; ++i) {
for (int j = 1; j <= N; ++j) {
dp[i][j] = (dp[i-1][j-1] * (N - (j - 1))) % mod;
if (j > K) {
dp[i][j] = (dp[i][j] + (dp[i-1][j] * (j - K)) % mod) % mod;
}
}
}
return (int)dp[L][N];
}
}

  

Analysis:

dp[i][j] denotes the solution of i songs with j difference songs. So the final answer should be dp[L][N]

Think one step before the last one, there are only cases for the answer of dp[i][j]

case 1 (the last added one is new song): listen i - 1 songs with j - 1 difference songs, then the last one is definitely new song with the choices of N - (j - 1).

case2 (the last added one is old song): listen i - 1 songs with j different songs, then the last one is definitely old song with the choices of j if without the constraint of K, the status equation will be dp[i][j] = dp[i-1][j-1] * (N - (j - 1)) + dp[i-1][j] * j

If with the constaint of K, there are also two cases

Case 1: no changes since the last added one is new song. Hence, there is no conflict

Case 2: now we don't have choices of j for the last added old song. Itt should be updateed j - k because k songs can't be chsed from j - 1 to j - k. However, if j <= K, this case will be 0 because only after choosing K different other songs, old song can be chosen.

if (j > k)

dp[i][j] = dp[i-1][j-1] * (N-(j-1)) + dp[i-1][j] * (j-k)

else

dp[i][j] = dp[i-1][j-1] * (N - (j-1))

Reference:

https://leetcode.com/problems/number-of-music-playlists/discuss/180338/DP-solution-that-is-Easy-to-understand

920. Number of Music Playlists的更多相关文章

  1. [LeetCode] 920. Number of Music Playlists 音乐播放列表的个数

    Your music player contains N different songs and she wants to listen to L (not necessarily different ...

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

    Your music player contains N different songs and she wants to listen to L (not necessarily different ...

  3. leetcode hard

    # Title Solution Acceptance Difficulty Frequency     4 Median of Two Sorted Arrays       27.2% Hard ...

  4. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  5. COM Error Code(HRESULT)部分摘录

    Return value/code Description 0x00030200 STG_S_CONVERTED The underlying file was converted to compou ...

  6. C#开发BIMFACE系列44 服务端API之计算图纸对比差异项来源自哪个图框

    BIMFACE二次开发系列目录     [已更新最新开发文章,点击查看详细] 在前两篇博客<C#开发BIMFACE系列42 服务端API之图纸对比>.<C#开发BIMFACE系列43 ...

  7. QNX 多线程 (线程1每隔20ms读取 number;线程2每隔10ms计算一次)

    #include <pthread.h>#include <stdio.h>#include <sys/time.h>#include <string.h&g ...

  8. JavaScript Math和Number对象

    目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...

  9. Harmonic Number(调和级数+欧拉常数)

    题意:求f(n)=1/1+1/2+1/3+1/4-1/n   (1 ≤ n ≤ 108).,精确到10-8    (原题在文末) 知识点:      调和级数(即f(n))至今没有一个完全正确的公式, ...

随机推荐

  1. Silverlight实用窍门系列:57.Silverlight中的Binding使用(二)-数据验证

    本文将简单讲述Silverlight中的Binding数据时的数据验证. NotifyOnValidationError:是否在出现异常/错误信息的时候激发BindingValidationError ...

  2. 在windows系统下安装oracle 11g

     oracle 11g 安装在windows server 2012 系统下. 最近,需要配置数据库,要求在windows操作系统下,安装oracle 11g 数据库,因为以前没有安装过,所以成功后, ...

  3. 模板练习(LUOGU)

    1:并查集 P3183食物链 #define man 300050 ; int find(int x){ if(fa[x]==x) return fa[x]; return fa[x]=find(fa ...

  4. UI设计,你为什么不能把标题做的更明显呢?

    在设计中标题常常被重视,标题即是文案信息的精华提炼,那么如何能把标题在很多文案信息中脱颖而出就是设计师所要做的工作,前面的文章说过对比可以凸显主题,这期是在对比合理的前提下更进一步的处理方法,我们可以 ...

  5. spring mvc MultipartFile 上传文件 当文件较小时(10k) ,无法上传成功 。

    <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 --> <bean id="multipartResolver" cla ...

  6. 经典递归问题:0,1背包问题 kmp 用遗传算法来解背包问题,hash表,位图法搜索,最长公共子序列

    0,1背包问题:我写笔记风格就是想到哪里写哪里,有很多是旧的也没删除,代码内部可能有很多重复的东西,但是保证能运行出最后效果 '''学点高大上的遗传算法''' '''首先是Np问题的定义: npc:多 ...

  7. a标签的四个伪类

    A标签的css样式   CSS为一些特殊效果准备了特定的工具,我们称之为“伪类”.其中有几项是我们经常用到的,下面我们就详细介绍一下经常用于定义链接样式的四个伪类,它们分别是: :link    :v ...

  8. Stacktraces java.lang.NoSuchMethodException: com.liuyang.action.UserAction.add()

    Struts Problem Report Struts has detected an unhandled exception: Messages: com.liuyang.action.UserA ...

  9. const define static extern 关键词详解

    const const关键词并不能把一个变量变成一个常量, 在符号前加上const表示这个符号不能被赋值, 即他的值对这个符号来说是只读的, 但并不代表这个值不能用其他方法去改变. 通过下面的例子就能 ...

  10. Robot Perception for Indoor Navigation《室内导航中的机器人感知》

    Felix Endres 论文下载 Technische Fakult¨ atAlbert-Ludwigs-Universit¨ at Freiburg Betreuer: Prof. Dr. Wol ...