1359. Count All Valid Pickup and Delivery Options LeetCode Solution
In this guide, you will get 1359. Count All Valid Pickup and Delivery Options LeetCode Solution with the best time and space complexity. The solution to Count All Valid Pickup and Delivery Options problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Count All Valid Pickup and Delivery Options solution in C++
- Count All Valid Pickup and Delivery Options solution in Java
- Count All Valid Pickup and Delivery Options solution in Python
- Additional Resources

Problem Statement of Count All Valid Pickup and Delivery Options
Given n orders, each order consists of a pickup and a delivery service.
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
Complexity Analysis
- Time Complexity:
- Space Complexity:
1359. Count All Valid Pickup and Delivery Options LeetCode Solution in C++
class Solution {
public:
int countOrders(int n) {
constexpr int kMod = 1'000'000'007;
long ans = 1;
for (int i = 1; i <= n; ++i)
ans = ans * i * (i * 2 - 1) % kMod;
return ans;
}
};
/* code provided by PROGIEZ */
1359. Count All Valid Pickup and Delivery Options LeetCode Solution in Java
class Solution {
public int countOrders(int n) {
final int kMod = 1_000_000_007;
long ans = 1;
for (int i = 1; i <= n; ++i)
ans = ans * i * (i * 2 - 1) % kMod;
return (int) ans;
}
}
// code provided by PROGIEZ
1359. Count All Valid Pickup and Delivery Options LeetCode Solution in Python
class Solution:
def countOrders(self, n: int) -> int:
kMod = 1_000_000_007
ans = 1
for i in range(1, n + 1):
ans = ans * i * (i * 2 - 1) % kMod
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.