题目如下:

解题思路:动态规划。记dp[i] = v表示由i个人组成的圈子一共有v种握手的方法。对于一个由n个人组成的圈子,编号为0的人一共可以和编号为 (1,3,5....,n-1)的握手,这也很好理解,假设编号为0的人和编号为2的人握手,那么编号为1的人就被包在两人的连线的一侧,而同侧没有其他人可以握手。假设编号为0的人和编号为i的人握手,可以理解成0~i之间的连线把其余人分成了两部分,一部分的人编号是1~i-1,另外一部分的编号是i+1~n-1,这两部分分别形成独立的圈子,所以编号为0的人和编号为i的人握手时,可以握手的组合的总数就应该dp[i-1] * dp[n-i-1],最后只要累计编号为0的人分别和编号为 (1,3,5....,n-1)的总数即可。这里有一个例外,就是0和1握手或者0和n-1握手,这种方式并没有把剩余的人分成两个圈子,而是一个圈子。

代码如下:

class Solution(object):
def numberOfWays(self, num_people):
"""
:type num_people: int
:rtype: int
"""
num_people += 1
dp = [0] * (num_people)
dp[2] = 1
for i in range(4,num_people,2):
for j in range(2,i+1,2):
left = (j - 1 - 1)
right = (i - j) if left == 0:
dp[i] += dp[right]
elif right == 0:
dp[i] += dp[left]
else:
dp[i] += dp[left] * dp[right]
return dp[-1] % (10**9 + 7)

【leetcode】1259.Handshakes That Don't Cross的更多相关文章

  1. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  2. 【Leetcode】Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  3. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  4. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  5. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  6. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  7. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  8. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

  9. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

随机推荐

  1. python selenium 实战涉及很多知识点

    1.iframe的切入和切出 #切入 driver.switch_to.frame(driver.find_element_by_id('iFrame_1')) # 切换出来 driver.switc ...

  2. 【Linux开发】linux设备驱动归纳总结(八):4.总线热插拔

    linux设备驱动归纳总结(八):4.总线热插拔 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...

  3. SQL 批量添加的语法

    .--添加一条记录 . insert into tableName(col1,col2,col3) values (val1,val2,val3) .--添加多条记录 . insert into ta ...

  4. 【Python】【demo实验1】【Python运行时强制刷新缓冲区,输出信息】(Python3 应不存在类似情况)

    [原文] 需求:打印一颗 ”*” 休息1s 代码如下: #!/usr/bin/python #coding=utf-8 ''' 暂停1s输出 ''' import time def printStar ...

  5. sql server之批量数据导入

    实际应用场景中,有时会需要把一批数据导入数据库.这批数据可能来源于另一个数据源.比较常规的做法是先读取到dataset,然后跑一个循环,每一行拼一句insert into语句,执行之.用过的人会知道, ...

  6. EBS自动行号,行金额自动汇总到头,金额根据币种编号总结

    一.自动行号实现 1.方法一: 只需要将“序号”定义成公式,并将公式设置为:get_block_property('block_name',current_record)就可以实现了,或者把这行语句放 ...

  7. 学习笔记--最近公共祖先(LCA)的几种求法

    前言: 给定一个有根树,若节点\(z\)是两节点\(x,y\)所有公共祖先深度最大的那一个,则称\(z\)是\(x,y\)的最近公共祖先(\(Least Common Ancestors\)),简称\ ...

  8. 三剑客-awk(简写)

    特殊要点:$0 表示整个当前行$1 每行第一个字段NF 字段数量变量NR 每行的记录号,多文件记录递增OFS 输出字段分隔符, 默认也是空格,可以改为制表符等ORS 输出的记录分隔符,默认为换行符,即 ...

  9. loj 2392「JOISC 2017 Day 1」烟花棒

    loj 答案显然满足二分性,先二分一个速度\(v\) 然后显然所有没有点火的都会往中间点火的人方向走,并且如果两个人相遇不会马上点火,要等到火快熄灭的时候才点火,所以这两个人之后应该在一起行动.另外有 ...

  10. Js阻止冒泡,Vue中如何阻止冒泡事件

    js解决冒泡:event.stopPropagation() vue解决冒泡: 事件.stop,例如:@click.stop="" ,@mouseover.stop="& ...