-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCount-all-valid.cpp
44 lines (36 loc) · 1.32 KB
/
Count-all-valid.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Solution {
private:
int MOD = 1e9 + 7;
vector<vector<int>> memo;
long totalWays(int unpicked, int undelivered) {
if (unpicked == 0 && undelivered == 0) {
// We have completed all orders.
return 1;
}
if (unpicked < 0 || undelivered < 0 || undelivered < unpicked) {
// We can't pick or deliver more than N items
// Number of deliveries can't exceed number of pickups
// as we can only deliver after a pickup.
return 0;
}
if (memo[unpicked][undelivered]) {
// Return cached value, if already present.
return memo[unpicked][undelivered];
}
long ans = 0;
// Count all choices of picking up an order.
ans += unpicked * totalWays(unpicked - 1, undelivered);
// Handle integer overflow.
ans %= MOD;
// Count all choices of delivering a picked order.
ans += (undelivered - unpicked) * totalWays(unpicked, undelivered - 1);
// Handle integer overflow.
ans %= MOD;
return memo[unpicked][undelivered] = ans;
}
public:
int countOrders(int n) {
memo = vector<vector<int>> (n + 1, vector<int>(n + 1, 0));
return totalWays(n, n);
}
};