[LeetCode] 1359. 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
這道題說是有n個訂單,每個訂單要安排提貨 pickup,和送貨 delivery 兩個事件,并且要求 pickup 必須在 delivery 事件前面,問n個訂單的所有 pickup 和 delivery 的合法排序有多少種。參見題目中的例子2不難理解題意,可以看出 Pi 一定在 Di 前面,實際上這道題是一道排列組合題,是要找規律的,若 n=1 時,則根據題目要求,只有一種排列,即 P1 D1,而當 n=2 時,P2 可以加入的位置有哪些呢,其實有3個位置可以加入,如下所示:
_ P1 _ D1 _
兩個字符共有3個加入位置,即 n * 2 - 1,若此時找個位置放下了 P2,則現在場上有了三個字符,理論上應該有4個加入位置,即 n * 2。又因為 P2 必須要在 D2 的前面,所以應該減少一半的情況,則總共有 3 * 4 / 2 = 6 種情況,即 (n * 2 - 1) * n * 2 / 2,化簡一下得到 (n * 2 - 1) * n,這個就是遞推公式,有了這個遞歸公式,就可以求出任意的n值了,注意別忘了結果要對 10^9 + 7 取余,參見代碼如下:
解法一:
class Solution {
public:
int countOrders(int n) {
long res = 1, M = 1e9 + 7;
for (int i = 1; i <= n; ++i) {
res = res * (i * 2 - 1) * i % M;
}
return res;
}
};
再來看一種遞歸的寫法,一行搞定碉堡了,注意這里面的類型轉換,相乘之前要轉為長整型 long 來避免溢出,參見代碼如下:
解法二:
class Solution {
public:
int countOrders(int n) {
return n > 0 ? ((long)countOrders(n - 1) * (n * 2 - 1) * n % ((long)1e9 + 7)) : 1;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/1359
參考資料:
https://leetcode.com/problems/count-all-valid-pickup-and-delivery-options


浙公網安備 33010602011771號