Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dcb2cb3

Browse files
committed
update 1359.count-all-valid-pickup-and-delivery-options.cpp
1 parent 0a9860e commit dcb2cb3

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* @lc app=leetcode id=1359 lang=cpp
3+
*
4+
* [1359] Count All Valid Pickup and Delivery Options
5+
*
6+
* https://leetcode.com/problems/count-all-valid-pickup-and-delivery-options/description/
7+
*
8+
* algorithms
9+
* Hard (58.25%)
10+
* Likes: 81
11+
* Dislikes: 11
12+
* Total Accepted: 3.5K
13+
* Total Submissions: 6K
14+
* Testcase Example: '1'
15+
*
16+
* Given n orders, each order consist in pickup and delivery services.
17+
*
18+
* Count all valid pickup/delivery possible sequences such that delivery(i) is
19+
* always after of pickup(i).
20+
*
21+
* Since the answer may be too large, return it modulo 10^9 + 7.
22+
*
23+
*
24+
* Example 1:
25+
*
26+
*
27+
* Input: n = 1
28+
* Output: 1
29+
* Explanation: Unique order (P1, D1), Delivery 1 always is after of Pickup
30+
* 1.
31+
*
32+
*
33+
* Example 2:
34+
*
35+
*
36+
* Input: n = 2
37+
* Output: 6
38+
* Explanation: All possible orders:
39+
* (P1,P2,D1,D2), (P1,P2,D2,D1), (P1,D1,P2,D2), (P2,P1,D1,D2), (P2,P1,D2,D1)
40+
* and (P2,D2,P1,D1).
41+
* This is an invalid order (P1,D2,P2,D1) because Pickup 2 is after of Delivery
42+
* 2.
43+
*
44+
*
45+
* Example 3:
46+
*
47+
*
48+
* Input: n = 3
49+
* Output: 90
50+
*
51+
*
52+
*
53+
* Constraints:
54+
*
55+
*
56+
* 1 <= n <= 500
57+
*
58+
*/
59+
60+
// @lc code=start
61+
class Solution {
62+
public:
63+
long long M;
64+
vector<int> dp;
65+
int f(long long n){
66+
if(n <= 1) return 1;
67+
68+
if(dp[n] != -1)
69+
return dp[n];
70+
71+
return dp[n] = n*(f(n-1) + (n-1)*(n*2-2)*(n*2-3)*f(n-2)%M)%M;
72+
}
73+
int countOrders(int n) {
74+
M = 1000000000+7;
75+
dp = vector<int>(n+1, -1);
76+
return f(n);
77+
}
78+
};
79+
// @lc code=end

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /