Leetcode: 1359. Count All Valid Pickup and Delivery Options
- How can we extend the solution for \(i\) to \(i+1\)? Select the order to pick and when it will be delivered. To delivery the rest of order, we have the solve the same problem with less one order. Time complexity is \(O(n)\) and space is \(O(1)\).
class Solution: def countOrders(self, n: int) -> int: MOD = 10**9 + 7 ans = 1 for i in range(2, n + 1): ans = ((i * (2 * i - 1) % MOD) * ans) % MOD return ans assert Solution().countOrders(1) == 1 assert Solution().countOrders(2) == 6 assert Solution().countOrders(3) == 90