Problem statement

https://leetcode.com/problems/count-all-valid-pickup-and-delivery-options/

Solution

Just do combinatorics solution: choose first 2 places, next 2 places and so on.

Complexity

Time complexity is O(n), space complexity is O(1).

Code

class Solution:
    def countOrders(self, n):
        ans = 1
        for i in range(1, n + 1):
            ans = ans * comb(2*i, 2) % (10**9 + 7)
        
        return ans