LeetCode 1259. Handshakes That Don't Cross - Java - DP
题目链接: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 <= 1000num_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 个人和第 i (i = n - 1, n - 3, ……,1)个人握手,那么分成的两部分中,一部分有 i - 1 人,另一部分有 n - i - 1 人。这两部分又是一个新的子问题。
所以题目可以采用 动态规划(DP) 来解决。
用大小为 num_people + 1 的 long 型一维数组 arr 来保存每种人数时的方案数。公式为:
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的更多相关文章
- 【leetcode】1259.Handshakes That Don't Cross
题目如下: 解题思路:动态规划.记dp[i] = v表示由i个人组成的圈子一共有v种握手的方法.对于一个由n个人组成的圈子,编号为0的人一共可以和编号为 (1,3,5....,n-1)的握手,这也很好 ...
- LeetCode高频题目(100)汇总-Java实现
LeetCode高频题目(100)汇总-Java实现 LeetCode高频题目(100)汇总-Java实现 目录 第01-50题 [Leetcode-easy-1] Two Sum [Le ...
- LeetCode 57. Insert Interval 插入区间 (C++/Java)
题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...
- LeetCode:零钱兑换【322】【DP】
LeetCode:零钱兑换[322][DP] 题目描述 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成 ...
- 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 ...
- LeetCode算法题-Subdomain Visit Count(Java实现)
这是悦乐书的第320次更新,第341篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第189题(顺位题号是811).像"discuss.leetcode.com& ...
- LeetCode算法题-Letter Case Permutation(Java实现)
这是悦乐书的第315次更新,第336篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第184题(顺位题号是784).给定一个字符串S,将每个字母单独转换为小写或大写以创建另 ...
- LeetCode算法题-Jewels and Stones(Java实现)
这是悦乐书的第313次更新,第334篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第182题(顺位题号是771).字符串J代表珠宝,S代表你拥有的石头.S中的每个字符都是 ...
- LeetCode算法题-Reach a Number(Java实现)
这是悦乐书的第310次更新,第331篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第179题(顺位题号是754).你站在无限数字线的0号位置.在目的地有个target.在 ...
随机推荐
- Pandas模块 -- 数据类型转换,描述统计
car=pd.read_csv(r'E:\Python\sec_cars.csv',sep=',').head(32) # print(car) print("数据集的类型:",t ...
- presto-gateway lyft 团队开源的prestodb 的负载均衡、代理、网关工具
presto-gateway 是 lyft 团队开源 的prestodb 的工具,很方便,我们可以用来方便的管理presto 多集群 通过yaml 进行配置管理,可以方便的管理不同的集群 lyft 参 ...
- 退役IV次后做题记录
退役IV次后做题记录 我啥都不会了.... AGC023 D 如果所有的楼房都在\(S\)同一边可以直接得出答案. 否则考虑最左最右两边的票数,如果左边>=右边,那么最右边会投给左边,因为就算车 ...
- vue-cli配置跨域代理
现在使用vue大多使用了前后端分离模式,因此游览器经常显示跨域失败的信息,现在跨域的方式很多种,主要分两大类,ajax跨域,dom跨域,具体的方法就不例举啦. vue-cli作为一个强大的脚手架,内置 ...
- 在交互环境下使用 Pyspark 提交任务给 Spark 解决 : java.sql.SQLException: No suitable driver
在 jupyter 上启用 local 交互环境和 spark 进行交互使用 imapla 来帮助 spark 取数据却失败了 from pyspark.sql import SparkSession ...
- SVN cornerstone Commit\Update 提示xxx is already Locked解决方法
点击菜单栏“working copy” 点“clean”: 或者,如下图: 右击,点击“clean”,问题解决.
- bind named.conf 的理解
[root@46 /]#yum -y install bind bind-chroot bind-libs bind-utils caching-nameserver目录说明/var/named/ch ...
- error: dereferencing pointer to incomplete type
/******************************************************************************* * error: dereferenc ...
- MLflow系列4:MLflow模型
英文链接:https://mlflow.org/docs/latest/models.html 本文链接:https://www.cnblogs.com/CheeseZH/p/11946260.htm ...
- vue中axios使用二:axios以post,get,jsonp的方式请求后台数据
本文为博主原创,转载请注明出处 axios在上一篇中讲过:vue中axios使用一:axios做拦截器,axios是请求后台资源的模块,用来请求后台资源. axios本身是支持get,post请求后台 ...