当前位置:网站首页>1259. Programmation dynamique de poignée de main disjointe
1259. Programmation dynamique de poignée de main disjointe
2022-07-23 18:41:00 【Madame Yu】
1259. Poignée de main disjointe
Nombre pair L'homme se tient dans un cercle,Nombre total de personnes
num_people.Tout le monde serre la main de quelqu'un d'autre que lui,Donc au total, il y auranum_people / 2Poignée de main secondaire.Connectez les gens qui se serrent la main,Veuillez retourner le nombre de schémas de poignée de main où les connexions ne se croisent pas.
Comme les résultats peuvent être importants,Veuillez retourner à la réponse Module
10^9+7Après les résultats.Exemple 1:
Entrée:num_people = 2 Produits:1Exemple 2:
Entrée:num_people = 4 Produits:2 Explication:Il y a deux options au total,La première option est [(1,2),(3,4)] ,La deuxième option est [(2,3),(4,1)] .Exemple 3:
Entrée:num_people = 6 Produits:5Exemple 4:
Entrée:num_people = 8 Produits:14Conseils:
2 <= num_people <= 1000num_people % 2 == 0
Résultats des questions
Succès, Cette question est probablement particulièrement simple dans les questions difficiles
Méthodes:Planification dynamique
- Supposons qu'il y ait x Particuliers, On a choisi l'un d'eux a,C'est a Serrer la main d'un homme , Pour assurer une solution ultérieure , Les deux parties séparées doivent être égales
- a Après la poignée de main ,Il en reste.,x-2 Personne ne serre la main ,x Après la poignée de main , Diviser le reste du personnel en deux parties par une ligne de poignée de main , Supposons qu'une partie soit y Particuliers, Le reste est x-2-y Particuliers
- C'est y Problème de poignée de main , Juste pour x Sous - question de la poignée de main ,Enumeration Tout ce qui est possible y Et x-2-y Possibilité de poignée de main , Les deux n'interfèrent pas , Traitement multiplicatif
Optimisation
- Parce que c'est un nombre pair , Tout ce qui compte, c'est le nombre de paires de mains , L'espace peut être réduit à n/2
- Un côté est divisé en y Les gens, Un côté est divisé en x-2-y Les gens, Même résultat que l'inverse , Alors, pour les deux nombres différents ,Peut être multiplié par2, Cela réduit de moitié le nombre de cycles ,En particulier,,Quand y=x-2-yHeure,Il n'y a qu'une seule possibilité.,
class Solution {
public int numberOfWays(int numPeople) {
int half = numPeople/2;
long[] dp = new long[half+1];
long MOD = (long) (1e9+7);
dp[0]=1;
for(int i = 1; i <= half; i++){
for(int j = 0; j < i-j-1; j++){
dp[i]+=dp[j]*dp[i-j-1]*2;
dp[i] = dp[i]%MOD;
}
if(i%2!=0){
dp[i]+=dp[i/2]*dp[i-i/2-1];
dp[i] = dp[i]%MOD;
}
}
return (int) dp[half];
}
}Complexité temporelle:O(n)
Complexité spatiale:O(n)
边栏推荐
- 【游戏建模模型制作全流程】ZBrush武器模型制作:弩
- MySQL classic exercises and answers, 50 common SQL sentence exercises
- ZigBee system development of Internet of things I (Introduction to ZigBee) [easy to understand]
- LeetCode 0131. 分割回文串
- 【Coggle 30 Days of ML】糖尿病遗传风险检测挑战赛(2)
- 代码整洁,高效的系统方法
- Does anyone get a job by self-study modeling? Don't let these thoughts hurt you
- 入门学习3D建模一般会遇到哪些问题?实在是太多了
- Awk from introduction to earth (16) comprehensive case of awk
- Jetpack Compose之Navigation组件使用
猜你喜欢

SQLZOO——BBC QUIZ

What happened behind kubectl's creation of pod?

如何理解:普通代码块、构造块、静态块?有什么关系?

LeetCode 剑指 Offer II 115.重建序列:图解 - 拓扑排序

How to replace the double quotation marks of Times New Roman in word with the double quotation marks in Tahoma

Three things programmers want to do most | comics

SQLZOO——SELECT from Nobel Tutorial

图的存储结构与方法(二)

入行3D建模有前景吗?就业高薪有保障还是副业接单赚钱多

大神“魔改”AirPods,配备USB-C接口,3D打印外壳让维修更容易
随机推荐
kubectl 创建 Pod 背后到底发生了什么?
从业务开发中学习和理解架构设计
JUC并发编程【详解及演示】
PCL: multi line fitting (RANSAC)
华为胖瘦AP切换方法
如何成为建模师?工业建模和游戏建模哪一个更加吃香?
ResponseBodyAdvice接口使用导致的报错及解决
LeetCode 0131. 分割回文串
LeetCode 每日一题 2022/7/18-2022/4/24
Prevent and control the summer market blowout after adjustment, and evaluate the summer activities of Tujia, muniao and meituan
Problems and methods of creating multiple projects under one solution in VS2010
Jetpack Compose之Navigation组件使用
Is it suitable for learning 3D modeling? You can't lose one of these five points
How to evaluate the accuracy of stock analysts' prediction?
我的创作纪念日
Paddlenlp之UIE分类模型【以情感倾向分析新闻分类为例】含智能标注方案)
Common problems of sklearn classifier
【2020】【论文笔记】相变材料与超表面——
Modeling at the beginning of learning is very confused, how to learn next generation role modeling?
【游戏建模模型制作全流程】用ZBrush制作游戏士兵角色

