LeetCode 1259. Handshakes That Don't 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

二、简要翻译

n(偶数)个人围成个圈,两两握手,要求不能交叉握手。求可能的握手方案个数(mod 10^9 + 7)

三、思路分析

  • 动态规划类问题。可以从上到下或者从下到上来解决这个问题。
  • 假设有n个人,数组result[i] 表示i个人构成的子问题的答案。
  • 第一个人只能和第2,4,6,8 ... 个人握手。根据握手结果将圆分成两个半球。
  • result[n] = result[0] * result[n-2] + result[2] * result[n-4] + result[4] * result[n-6] +...+ result[n-2] * result[0]
  • 循环计算的时候注意要mod 10^9 + 7
  • 这应该是分类成medium的问题吧。

四、代码

public int numberOfWays(int num_people) {
        int mod = (int) 1e9 + 7;
        int len = num_people / 2;
        long[] results = new long[len + 1];
        results[0] = 1;
        results[1] = 1;
        long result;
        for (int i = 2; i <= len; i++) {
            result = 0;
            for (int j = 1; j <= i; j++) {
                result += (results[j - 1] * results[i - j]) % mod;
                result %= mod;
            }
            results[i] = result;
        }
        return (int) results[len];
    }

  

LeetCode 1259. Handshakes That Don't Cross

原文地址:https://www.cnblogs.com/onePunchCoder/p/11875773.html

时间: 2024-11-08 10:24:15

LeetCode 1259. Handshakes That Don't Cross的相关文章

LeetCode 1259. Handshakes That Don&#39;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 t

Leetcode: Convex Polygon

Given a list of points that form a polygon when joined sequentially, find if this polygon is convex (Convex polygon definition). Note: There are at least 3 and at most 10,000 points. Coordinates are in the range -10,000 to 10,000. You may assume the

Leetcode: Perfect Rectangle

Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover of a rectangular region. Each rectangle is represented as a bottom-left point and a top-right point. For example, a unit square is represented as [1,1,2,2

[LeetCode] Frog Jump 青蛙过河

A frog is crossing a river. The river is divided into x units and at each unit there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water. Given a list of stones' positions (in units) in sorted ascending ord

LeetCode Frog Jump

原题链接在这里:https://leetcode.com/problems/frog-jump/description/ 题目: A frog is crossing a river. The river is divided into x units and at each unit there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water. Giv

[LeetCode] 349 Intersection of Two Arrays &amp; 350 Intersection of Two Arrays II

这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/ 350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 题目&解法

LeetCode 442. Find All Duplicates in an Array (在数组中找到所有的重复项)

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime? Example: Input: [4,3,2,7,

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro

LeetCode OJ - Longest Consecutive Sequence

这道题中要求时间复杂度为O(n),首先我们可以知道的是,如果先对数组排序再计算其最长连续序列的时间复杂度是O(nlogn),所以不能用排序的方法.我一开始想是不是应该用动态规划来解,发现其并不符合动态规划的特征.最后采用类似于LRU_Cache中出现的数据结构(集快速查询和顺序遍历两大优点于一身)来解决问题.具体来说其数据结构是HashMap<Integer,LNode>,key是数组中的元素,所有连续的元素可以通过LNode的next指针相连起来. 总体思路是,顺序遍历输入的数组元素,对每个