【leetcode】1259.Handshakes That Don't Cross
题目如下:
解题思路:动态规划。记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的更多相关文章
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【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 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
随机推荐
- Python pip升级及升级失败解决方案
本教程用于Python pip升级及失败解决方案 首先查看脚本 pip show pip 我已经升级到了最新的版本 安装其他模块过程中出现下面提示,便说明你需要升级pip You are using ...
- 【linux开发】IO端口和IO内存的区别及分别使用的函数接口
IO端口和IO内存的区别及分别使用的函数接口 每个外设都是通过读写其寄存器来控制的.外设寄存器也称为I/O端口,通常包括:控制寄存器.状态寄存器和数据寄存器三大类.根据访问外设寄存器的不同方式,可以把 ...
- Ubuntu - Ubuntu应用记录(1)
1.发生dpkg status database is locked by another process 原因是包管理器没有正确关闭.需要重启计算机或者重新打开终端 输入: sudo rm /var ...
- 移除django的旧版本
移除django的旧版本 下面这一段代码打进去绝对能看到你想要的,根据这个路径去找版本文件夹,他的名字应该是django.2xx.xxx很长一段,请你删了它! import django import ...
- gson 带泛型的转换
json转对象 public static <T> T json2Obj(String json, Class<T> cls) { Gson gson = new Gson() ...
- oa_mvc_easyui_项目搭建及登录页面验证码(1)
1.空项目的搭建,三层的搭建(各层之中的引用) webapp:bll,model,common bll:dal,model dal:model 2.SQL表 ItcastDb:T_UserInfo,T ...
- Binding的简单使用
Binding可以看作是数据的桥梁,两端分别为Source和Target,一般情况,Source是逻辑层的对象,Target是UI层的控件对象,可以将数据从逻辑层送往UI层展现 简单的例子: clas ...
- 解决Idea、WebStorm下使用Vue cli脚手架项目无法使用Webpack别名的问题
问题截图: 解决方案: 1.打开File --> Setting 窗口 2.搜索 Webpack 3.选择如下路径 问题解决
- Axure(一)
axure1.原型工具 2.软件开发 1.可行性分析2.需求分析 产品经理(和甲方对接需求,) 乙方 -- 甲方 ps(专业性强,精美) 设计师 html(可变 ...
- vue-transition实现加入购物车效果及其他动画效果实现
vue提供了<transition></transition>和<transition-group></transition-group>实现元素状态的 ...