码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode每日一题(920. Number of Music Playlists)


    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

    抄的官方的答案。
    dp[l][n]是 l 首歌的歌单, n 首歌的排列组合数量, 那 dp[l][n]是由两部分组合起来的, 一部分是 dp[l-1][n-1]外加一首 n-1 之外的新歌, 另一部分是 dp[l-1][n]外加 n 当中的一首歌的重复, 第一种情况我们可以在 n-1 之外选择任意一首歌加到歌单中, 所以可选歌的数量是 total-(n-1), 第二种情况我们要判断前面是否已经播放了 k 首不同的歌, 所以我们要比较 n 和 k 的大小, 只有 n > k 的时候我们才能将这种情况下的排列组合数量加到答案中。


    impl Solution {
        pub fn num_music_playlists(n: i32, goal: i32, k: i32) -> i32 {
            let mut dp = vec![vec![0; n as usize + 1]; goal as usize + 1];
            dp[0][0] = 1;
            for i in 1..=goal {
                for j in 1..=n {
                    dp[i as usize][j as usize] +=
                        dp[i as usize - 1][j as usize - 1] * (n - j + 1) as i64;
                    dp[i as usize][j as usize] +=
                        dp[i as usize - 1][j as usize] * (j - k).max(0) as i64;
                    dp[i as usize][j as usize] %= 1000000007;
                }
            }
            dp[goal as usize][n as usize] as i32
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
  • 相关阅读:
    洛谷P4560 Wall 砖墙
    反射、枚举及lambda表达式
    HTML学生个人网站作业设计 明星易烊千玺介绍(HTML+CSS) web前端开发技术 web课程设计 网页规划与设计
    非零基础自学Java (老师:韩顺平) 第10章 面向对象编程(高级部分) 10.7 抽象类最佳实践 - 模板设计模式
    3. JVM-运行时数据区概述及线程
    leetcode 11-盛最多水的容器
    SpringCloud集成Sleuth和Zipkin
    架构每日一学 14:架构师如何进行可行性探索?
    【Git】IDEA 集成 GitHub
    大厂面试-美团高频考察算法之重排链表
  • 原文地址:https://blog.csdn.net/wangjun861205/article/details/127818142
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号