码迷,mamicode.com
首页 > 其他好文 > 详细

1359. Count All Valid Pickup and Delivery Options

时间:2020-02-25 13:02:43      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:services   style   java   info   https   img   discus   example   lan   

Given n orders, each order consist in pickup and delivery services. 

Count all valid pickup/delivery possible sequences such that delivery(i) is always after of pickup(i). 

Since the answer may be too large, return it modulo 10^9 + 7.

 

Example 1:

Input: n = 1
Output: 1
Explanation: Unique order (P1, D1), Delivery 1 always is after of Pickup 1.

Example 2:

Input: n = 2
Output: 6
Explanation: All possible orders: 
(P1,P2,D1,D2), (P1,P2,D2,D1), (P1,D1,P2,D2), (P2,P1,D1,D2), (P2,P1,D2,D1) and (P2,D2,P1,D1).
This is an invalid order (P1,D2,P2,D1) because Pickup 2 is after of Delivery 2.

Example 3:

Input: n = 3
Output: 90

 

Constraints:

  • 1 <= n <= 500
class Solution {
    public int countOrders(int n) {
        long res = 1, mod = (long)1e9 + 7;
        for (int i = 1; i <= n; ++i)
            res = res * (i * 2 - 1) * i % mod;
        return (int)res;
    }
}

技术图片

 

 https://leetcode.com/problems/count-all-valid-pickup-and-delivery-options/discuss/516968/JavaC%2B%2BPython-Easy-and-Concise

技术图片

 

 

class Solution {
    public int countOrders(int n) {
        long MODULO = (long) (1e9 + 7);
        long ans = 1;
        for (int i = 2; i <= n; i++) {
            int spaceNum = (i-1)*2 + 1;
            ans *= spaceNum * (spaceNum + 1) / 2;
            ans %= MODULO;
        }
        return (int) ans;
    }
}

 

1359. Count All Valid Pickup and Delivery Options

标签:services   style   java   info   https   img   discus   example   lan   

原文地址:https://www.cnblogs.com/wentiliangkaihua/p/12360982.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!