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 <= 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
个人和第 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.在 ...
随机推荐
- c# 如何进行动态加载dll
最近遇到了在c#中如何进行动态加载dll的话,搞定了,下面介绍一下自己的步骤. 1,新建dll. 打开vs,新建project->Class Library->项目名为testdll1.在 ...
- openjudge1.2
目录 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.2.10 1.2.1 描述 分别定义int,short类型的变量各一个,并依次输出 ...
- hosts 屏蔽广告 定位
hosts 屏蔽广告 定位 JS Miner 挖矿 百度全家桶的全天候定位记录 各类统计服务(仅屏蔽 JS.不屏蔽控制台) 常见下载劫持 360 和百度的部分软件下载 CNNIC 根证书劫持 http ...
- 【Gamma】Scrum Meeting 1
目录 写在前面 gamma阶段角色更换 进度情况 任务进度表 Gamma阶段燃尽图 遇到的困难 照片 代码Commit记录 后端 前端 写在前面 例会时间:5.25 22:30-23:00 例会地点: ...
- 【译文】走出Java ClassLoader迷宫 Find a way out of the ClassLoader maze
本文是一篇译文.原文:Find a way out of the ClassLoader maze 对于类加载器,普通Java应用开发人员不需要了解太多.但对于系统开发人员,正确理解Java的类加载器 ...
- template cannot be keyed. Place the key on real elements instead.
template cannot be keyed. Place the key on real elements instead. 一.总结 一句话总结: 原因:vue不支持在 template 元素 ...
- Postman 插件安装和使用
1.google商店 搜索“谷歌访问助手” 2.在商店搜索Postman 3.安装Postman 4.访问chrome://apps/ 5.点击postman
- Spring Cloud-Eureka 服务注册中心
Eureka 是 Netflix 开发的,一个基于 REST 服务的,服务注册与发现的组件 它主要包括两个组件:Eureka Server 和 Eureka Client Eureka Client: ...
- RSA加密和数字签名在Java中常见应用【原创】
相关术语解释: RSA,参考: https://en.wikipedia.org/wiki/RSA_(cryptosystem) 非对称加密算法 ,参考:https://baike.baidu.com ...
- Qt 组合框QComboBox的定制
转载:https://www.cnblogs.com/csuftzzk/p/qss_combobox.html 转载:https://www.bbsmax.com/A/E35pLgRK5v/ 转载:h ...