920. Number of Music Playlists
Your music player contains
N
different songs and she wants to listen toL
(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 playedReturn 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:
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:
920. Number of Music Playlists的更多相关文章
- [LeetCode] 920. Number of Music Playlists 音乐播放列表的个数
Your music player contains N different songs and she wants to listen to L (not necessarily different ...
- [Swift]LeetCode920. 播放列表的数量 | Number of Music Playlists
Your music player contains N different songs and she wants to listen to L (not necessarily different ...
- leetcode hard
# Title Solution Acceptance Difficulty Frequency 4 Median of Two Sorted Arrays 27.2% Hard ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- COM Error Code(HRESULT)部分摘录
Return value/code Description 0x00030200 STG_S_CONVERTED The underlying file was converted to compou ...
- C#开发BIMFACE系列44 服务端API之计算图纸对比差异项来源自哪个图框
BIMFACE二次开发系列目录 [已更新最新开发文章,点击查看详细] 在前两篇博客<C#开发BIMFACE系列42 服务端API之图纸对比>.<C#开发BIMFACE系列43 ...
- QNX 多线程 (线程1每隔20ms读取 number;线程2每隔10ms计算一次)
#include <pthread.h>#include <stdio.h>#include <sys/time.h>#include <string.h&g ...
- JavaScript Math和Number对象
目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...
- Harmonic Number(调和级数+欧拉常数)
题意:求f(n)=1/1+1/2+1/3+1/4-1/n (1 ≤ n ≤ 108).,精确到10-8 (原题在文末) 知识点: 调和级数(即f(n))至今没有一个完全正确的公式, ...
随机推荐
- Python打杂之路
1.任务要落到纸上好记性不如烂笔头,再好的记性也不如写到纸上明确无误,写到纸上就不用担心会漏掉哪项工作.平时,我们总是在忙着一项工作的同时还惦记着下一项工作,把工作都记下后,我们就可以专注于一项工作, ...
- 02 请求库之 selenium模块
selenium模块 一 介绍 selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题 selenium本质是通过驱动 ...
- 学美工、平面设计、UI设计,哪个有前途?
首先,在分析学美工.平面设计.UI设计,哪一个更有前途的时候,先要对三类设计有一个大概的了解. 比如“学美工.平面设计.UI设计是什么”等知识,才能更好地去进行选择. 01 阐述美工.平面设计.UI设 ...
- 【Sikuli】Sikuli 文档
http://sikulix-2014.readthedocs.io/en/latest/index.html
- Executor(一)ExecutorService 线程池
Executor(一)ExecutorService 线程池 本篇主要涉及到的是 java.util.concurrent 包中的 ExecutorService.ExecutorService 就是 ...
- tomcat 时间相差8个小时,百度上查到的,备份下
通常网上一查都是 修改 tomcat 的参数 ,如catalina 文件,jvm parameters 等.如果都不起作用,可以使用如下方式.. 你可以修改jdk的时间校正了,你这么来.进入 \hom ...
- classification report 使用
别人写的,但是还是有些不清晰,我最后补上了 最后一行:第一个0.7=(0.5*1+0*1+1*3)/5 其他类似 support行:在真实数据中y_ture中class 0有一个 class 1有1 ...
- 【commons-httpclient】Java中HttpClient工具访问Web请求
注意jar包是: HttpClient工具使用 HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程 ...
- 2018.10.08 NOIP模拟 斐波那契(贪心+hash/map)
传送门 签到题. 显然是可以贪心分组的,也就是尽量跟当前的分成一组. 这时我们需要判断a[l]+a[r],a[l+1]+a[r]...a[r−1]+a[r]a[l]+a[r],a[l+1]+a[r]. ...
- 2018.07.01洛谷P2617 Dynamic Rankings(带修主席树)
P2617 Dynamic Rankings 题目描述 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i ...