题目链接https://leetcode-cn.com/problems/handshakes-that-dont-cross/


You are given an even number of people num_people that stand around a circle and each person shakes hands with someone else, so that there are num_people / 2 handshakes total.

Return the number of ways these handshakes could occur such that none of the handshakes cross.

Since this number could be very big, return the answer mod 10^9 + 7

Example 1:

Input: num_people = 2
Output: 1

Example 2:

Input: num_people = 4
Output: 2
Explanation: There are two ways to do it, the first way is [(1,2),(3,4)] and the second one is [(2,3),(4,1)].

Example 3:

Input: num_people = 6
Output: 5

Example 4:

Input: num_people = 8
Output: 14

Constraints:

  • 2 <= num_people <= 1000
  • num_people % 2 == 0

题解

先分析一下示例 3,

第 6 个人和第 5 个人握手,圆被分成两部分,一部分是 4 个人,另一部分是 0 个人。0 个人的方案数为 1,4 个人的方案数可以递归计算为 2,所以这种情况有 2 种方案。

第 6 个人和第 3 个人握手,圆被分成两部分,每部分都是 2 个人,2 个人的方案数是 1,所以这种情况有 1 种方案。

第 6 个人和第 1 个人握手,圆被分成两部分,一部分是 0 个人,另一部分是 4 个人,所以这种情况有 2 中方案。

因此 6 个人的时候有 5 种方案数。@wowpH

n 个人(n为偶数),如果第 n 个人和第 ii = n - 1, n - 3, ……,1)个人握手,那么分成的两部分中,一部分有 i - 1 人,另一部分有 n - i - 1 人。这两部分又是一个新的子问题。

所以题目可以采用 动态规划(DP) 来解决。

用大小为 num_people + 1long 型一维数组 arr 来保存每种人数时的方案数。公式为:

\[arr[n] = \begin{cases}
1 &\text{ } n=0或n=2 \\
\displaystyle\sum_{i=1}^{n-1} (arr[i - 1] * arr[n - i - 1]) &\text{ } n>2,n为偶数,i为奇数
\end{cases}.\]


Java代码

/**
* @description 5125. Handshakes That Don't Cross
* @time 10ms
* @version 1.1
* @author wowpH
* @date 2019-11-17 22:44:21
*/
class Solution {
private static final int mod = 1000000007;
private long[] arr; public int numberOfWays(int num_people) {
arr = new long[num_people + 1];
return (int) dp(num_people);
} private long dp(int n) {
if (n == 0 || n == 2) {
return 1;
}
long ret = 0;
for (int i = n - 1; i >= 1; i -= 2) {
if (arr[i - 1] == 0) {
arr[i - 1] = dp(i - 1);
}
if (arr[n - i - 1] == 0) {
arr[n - i - 1] = dp(n - i - 1);
}
ret += arr[i - 1] * arr[n - i - 1];
ret %= mod;
}
return ret;
}
}

原文链接https://www.cnblogs.com/wowpH/p/11880952.html


- wowpH -

LeetCode 1259. Handshakes That Don't Cross - Java - DP的更多相关文章

  1. 【leetcode】1259.Handshakes That Don't Cross

    题目如下: 解题思路:动态规划.记dp[i] = v表示由i个人组成的圈子一共有v种握手的方法.对于一个由n个人组成的圈子,编号为0的人一共可以和编号为 (1,3,5....,n-1)的握手,这也很好 ...

  2. LeetCode高频题目(100)汇总-Java实现

    LeetCode高频题目(100)汇总-Java实现       LeetCode高频题目(100)汇总-Java实现 目录 第01-50题 [Leetcode-easy-1] Two Sum [Le ...

  3. LeetCode 57. Insert Interval 插入区间 (C++/Java)

    题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...

  4. LeetCode:零钱兑换【322】【DP】

    LeetCode:零钱兑换[322][DP] 题目描述 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成 ...

  5. leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution)

    leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution) On a staircase, the i-th step ...

  6. LeetCode算法题-Subdomain Visit Count(Java实现)

    这是悦乐书的第320次更新,第341篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第189题(顺位题号是811).像"discuss.leetcode.com& ...

  7. LeetCode算法题-Letter Case Permutation(Java实现)

    这是悦乐书的第315次更新,第336篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第184题(顺位题号是784).给定一个字符串S,将每个字母单独转换为小写或大写以创建另 ...

  8. LeetCode算法题-Jewels and Stones(Java实现)

    这是悦乐书的第313次更新,第334篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第182题(顺位题号是771).字符串J代表珠宝,S代表你拥有的石头.S中的每个字符都是 ...

  9. LeetCode算法题-Reach a Number(Java实现)

    这是悦乐书的第310次更新,第331篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第179题(顺位题号是754).你站在无限数字线的0号位置.在目的地有个target.在 ...

随机推荐

  1. match 和 search 和 indexOf 查找及 正则表达式的 exec 和 test 用法

    function test(){ var name= "1.087"; var abc = "abd wor66k ne78xt"; var reg = /\d ...

  2. map访问key不存在的情况下,用find。比[]直接访问的意思不一样,map[key]不返null

    key不存在的话则创建一个pair并调用默认构造函数 map<CGuid, CLibItem>::iterator iterItem = m_world->m_library_sce ...

  3. git 初始用法

    Git global setup git config --global user.name "xiaoming" git config --global user.email & ...

  4. java项目里classpath具体指哪儿个路径

    一.classpath路径指什么 只知道把配置文件如:mybatis.xml.spring-web.xml.applicationContext.xml等放到src目录(就是存放代码.java文件的目 ...

  5. Python开发:OpenCV版本差异所引发的cv2.findContours()函数传参问题

    一.问题如下: cv2.findContours()这个方法是用来找出轮廓值的: # cv2.findContours找出轮廓值,cv2.RETR_EXTERNAL表示图像的外轮廓 binary, c ...

  6. [基础不过关填坑] 跨iframe触发事件

    子iframe $("#testId").on("change",function(){ alert("change") }) 父页面 $( ...

  7. Apache Kylin 简介

    http://kylin.apache.org/docs/index.html https://www.infoq.cn/article/vOrjsJCgVAVPim5hsj6p Kylin 的核心思 ...

  8. Metasploit使用内网跳板, 扫描局域网主机

    最近,拿到一台内网机器, 苦于无法使用nmap扫描改主机的内网, 所以才有此文 在跳板机子获取一定权限后,需要积极的向内网主机权限发展,获取指定的目标信息,探查系统漏洞,借助msf已经得到的meter ...

  9. Sublime Text 3.2.1详细安装破解教程,附最新激活码license(全网独家可用有效)

    title: "Sublime Text 3.2.1详细安装破解教程,附最新激活码license(全网独家可用有效)" categories: soft tags: soft au ...

  10. Win7下msys64安装mingw工具链

    1. 安装msys64 安装到指定目录, 例如C:\msys64 2. 命令行更新 运行msys2.exe打开命令行窗口, 执行命令 pacman -Syuu 3. 修改安装源 进入msys64/et ...