1359. Count All Valid Pickup and Delivery Options

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

Rust Solution

struct Solution;

const MOD: i64 = 1_000_000_007;

impl Solution {
    fn count_orders(n: i32) -> i32 {
        let n = n as i64;
        let mut res: i64 = 1;
        for i in 1..=n {
            res *= i * 2 - 1;
            res %= MOD;
            res *= i;
            res %= MOD;
        }
        res as i32
    }
}

#[test]
fn test() {
    let n = 1;
    let res = 1;
    assert_eq!(Solution::count_orders(n), res);
    let n = 2;
    let res = 6;
    assert_eq!(Solution::count_orders(n), res);
    let n = 3;
    let res = 90;
    assert_eq!(Solution::count_orders(n), res);
}

Having problems with this solution? Click here to submit an issue on github.