• LeetCode每日一题(1621. Number of Sets of K Non-Overlapping Line Segments)


    Given n points on a 1-D plane, where the ith point (from 0 to n-1) is at x = i, find the number of ways we can draw exactly k non-overlapping line segments such that each segment covers two or more points. The endpoints of each segment must have integral coordinates. The k line segments do not have to cover all n points, and they are allowed to share endpoints.

    Return the number of ways we can draw k non-overlapping line segments. Since this number can be huge, return it modulo 109 + 7.

    Example 1:

    Input: n = 4, k = 2
    Output: 5

    Explanation: The two line segments are shown in red and blue.
    The image above shows the 5 different ways {(0,2),(2,3)}, {(0,1),(1,3)}, {(0,1),(2,3)}, {(1,2),(2,3)}, {(0,1),(1,2)}.

    Example 2:

    Input: n = 3, k = 1
    Output: 3

    Explanation: The 3 ways are {(0,1)}, {(0,2)}, {(1,2)}.

    Example 3:

    Input: n = 30, k = 7
    Output: 796297179

    Explanation: The total number of possible ways to draw 7 line segments is 3796297200. Taking this number modulo 109 + 7 gives us 796297179.

    Constraints:

    • 2 <= n <= 1000
    • 1 <= k <= n-1

    首先这题我们要按线段来看, n 个点分割出 n-1 个线段, 每个线段要么是线, 要么是空。
    假设 dp[n][k][0]是第 n 条线段为空时且此时一共有 k 条线的情况的组合数量, dp[n][k][1]是第 n 条线段为线且此时一共有 k 条线的情况的组合数量, 我们不难推出, dp[n][k][0] = dp[n-1][k][0] + dp[n-1][k][1], 也就是前一个线段时不管是空还是线,已存在 k 条线的组合数量, 之所以需要 n-1 时就完成 k 条线是因为此时考虑的是第 n 段为空的情况。第 n 段为线的情况是 dp[n][k][1] = dp[n-1][k-1][0] + dp[n-1][k-1][1] + dp[n-1][k][1], 这里面 dp[n-1][k-1][0]是第 n-1 段为空且有 k-1 条线的情况, dp[n-1][k-1][1]是第 n-1 段为线且有 k-1 条线的情况, 这两个比较好理解,就是到第 n-1 段为止已经完成了 k-1 条线,第 n 段作为一条长度为 1 的独立的线加入。dp[n-1][k][1]所考虑的情况是第 n-1 段已经有了 k 条线而且第 n-1 段为线的情况,此时第 n 段为线,但不是独立的线,是跟 n-1 这一段融合的线,看做是 n-1 段的延长

    这题中等难度,但是做出来还是挺有成就感的


    
                    
  • 相关阅读:
    实验9(交换综合实验)
    Java多线程之相关拓展(静态代理和Lamda表达式演变)
    Vue3响应系统的实现(二)
    Nvidia GPU 入门教程之 11 数据中心 GPU 的对决:A100 与 V100S
    static静态关键字和继承
    pcl --- 如何估计点云中的表面法线
    安装typescript环境并开启VSCode自动监视编译ts文件为js文件
    神经网络图怎么分析,画神经网络结构图
    如何快速搭建一个大模型?简单的UI实现
    一文整理深度学习【深度学习linux的docker+pytorch+cuda+nvidia-docker+vscode配置】
  • 原文地址:https://blog.csdn.net/wangjun861205/article/details/126433719