题目链接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. 编译lua固件NodeMcu 8266

    https://www.cnblogs.com/yangfengwu/p/10547024.html 因为我现在的Wifi的教程是lua语言编写的,但是有些功能需要自己编译lua固件才可以,这篇文章就 ...

  2. 第04组alpha冲刺(3/4)

    队名:斗地组 组长博客:地址 作业博客:Alpha冲刺(3/4) 各组员情况 林涛(组长) 过去两天完成了哪些任务: 1.收集各个组员的进度 2.写博客 展示GitHub当日代码/文档签入记录: 接下 ...

  3. ImportError: cannot import name 'DjangoSuitConfig'

    pip3.6 install https://github.com/darklow/django-suit/tarball/v2

  4. SQL on Hadoop技术综述

    一.系统架构 runtime framework v.s. mpp 在SQL on Hadoop系统中,有两种架构: 1.一种是基于某个运行时框架来构建查询引擎,典型案例是Hive: 2.另一种是仿照 ...

  5. MORMOT REST文件上传

    MORMOT REST文件上传 上传数据格式必须是:MULTIPART/FORM-DATA uses SynCommons 后端处理参照下列代码: 后端处理参照下列代码: procedure TWeb ...

  6. Pushing state-of-the-art in 3D content understanding

    Pushing state-of-the-art in 3D content understanding 2019-10-31 06:34:08 This blog is copied from: h ...

  7. 解析prototxt文件的python库 prototxt-parser(使用parsy自定义文件格式解析)

    解析prototxt文件的python库 prototxt-parser https://github.com/yogin16/prototxt_parser https://test.pypi.or ...

  8. 【用例篇】Xmind转为csv 导入禅道

    用过禅道的都知道,用例维护实在是太不方便了,有人推荐了一种方法,用Xmind先编写用例(思路比较清晰),写好之后借助工具(xmind2testcase)将用例转为CSV格式,之后再导入禅道 参考资料: ...

  9. jetty源码下载

    jetty下载地址:https://www.eclipse.org/jetty/download.html Release         9.4.20.v20190813 .zip .tgz api ...

  10. css3特效插件wow.js

    在使用css3写特效的时候,会遇到比较麻烦的就是css3代码需要大量的调试,但是现在有了wow.js,让写特效变得简单了很多. wow.js官网 https://www.delac.io/wow/in ...