1359. Count All Valid Pickup and Delivery Options
Problem Statement
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
Intuition
Approach:
n=2
_P1_D1_
No for P2 D2
We have folowing options
1st position
P2-> D2 can sit on 1 2 3
2nd
2 3
3rd
3
3 2 1 = 6 options
therefore x*(x+1)/2 = 6 find x
But what is x
1 1
2 3
3 5
x = 2*(n-1)
Hence do a recusion
Links
https://leetcode.com/problems/count-all-valid-pickup-and-delivery-options/description/
Video Links
Approach 1:
class Solution {
public:
long long mod = 1e9+7;
vector<int> dp;
long long solve(int n){
if(n==1||n==0)
return 1;
else if( dp[n] != -1)return dp[n];
return dp[n] = (n*(2*n-1)*solve(n-1))%mod;
}
int countOrders(int n) {
dp.resize(1001 , -1);
return solve(n);
}
};
Approach 2:
Approach 3:
Approach 4:
Similar Problems
Last updated