-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
find-the-count-of-monotonic-pairs-i.cpp
130 lines (122 loc) · 4.54 KB
/
find-the-count-of-monotonic-pairs-i.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Time: O(n + r), r = max(nums)
// Space: O(n + r)
// combinatorics, stars and bars
class Solution {
public:
int countOfPairs(vector<int>& nums) {
// arr1 = [0+x1, arr1[0]+max(nums[1]-nums[0], 0)+x2, ..., arr[n-2]+max(nums[n-1]-nums[n-2], 0)+xn]
// => sum(max(nums[i]-nums[i-1], 0) for i in xrange(1, len(nums)))+(x1+x2+...+xn) <= nums[-1]
// => x1+x2+...+xn <= nums[-1]-sum(max(nums[i]-nums[i-1], 0) for i in xrange(1, len(nums))) = cnt <= min(nums)
// => the answer is the number of solutions s.t. x1+x2+...+xn <= cnt, where cnt >= 0
int cnt = nums.back();
for (int i = 1; i < size(nums); ++i) {
cnt -= max(nums[i] - nums[i - 1], 0);
}
return cnt >= 0 ? nHr(size(nums) + 1, cnt) : 0;
}
private:
int nHr(int n, int r) {
return nCr(n + r - 1, r);
}
int nCr(int n, int k) {
while (size(inv_) <= n) { // lazy initialization
fact_.emplace_back(mulmod(fact_.back(), size(inv_)));
inv_.emplace_back(mulmod(inv_[MOD % size(inv_)], MOD - MOD / size(inv_))); // https://cp-algorithms.com/algebra/module-inverse.html
inv_fact_.emplace_back(mulmod(inv_fact_.back(), inv_.back()));
}
return mulmod(mulmod(fact_[n], inv_fact_[n - k]), inv_fact_[k]);
}
uint32_t addmod(uint32_t a, uint32_t b) { // avoid overflow
a %= MOD, b %= MOD;
if (MOD - a <= b) {
b -= MOD; // relied on unsigned integer overflow in order to give the expected results
}
return a + b;
}
// reference: https://stackoverflow.com/questions/12168348/ways-to-do-modulo-multiplication-with-primitive-types
uint32_t mulmod(uint32_t a, uint32_t b) { // avoid overflow
a %= MOD, b %= MOD;
uint32_t result = 0;
if (a < b) {
swap(a, b);
}
while (b > 0) {
if (b & 1) {
result = addmod(result, a);
}
a = addmod(a, a);
b >>= 1;
}
return result;
}
static const uint32_t MOD = 1e9 + 7;
vector<int> fact_ = {1, 1};
vector<int> inv_ = {1, 1};
vector<int> inv_fact_ = {1, 1};
};
// Time: O(n * r), r = max(nums)
// Space: O(r)
// dp, prefix sum
class Solution2 {
public:
int countOfPairs(vector<int>& nums) {
static const int MOD = 1e9 + 7;
vector<int> dp(ranges::max(nums) + 1); // dp[j]: numbers of arr1, which is of length i+1 and arr1[i] is j
for (int i = 0; i <= nums[0]; ++i) {
dp[i] = 1;
}
for (int i = 1; i < size(nums); ++i) {
// arr1[i-1] <= arr1[i]
// => arr1[i]-arr1[i-1] >= 0 (1)
//
// arr2[i-1] >= arr2[i]
// => nums[i-1]-arr1[i-1] >= nums[i]-arr1[i]
// => arr1[i]-arr1[i-1] >= nums[i]-nums[i-1] (2)
//
// (1)+(2): arr1[i]-arr1[i-1] >= max(nums[i]-nums[i-1], 0)
vector<int> new_dp(size(dp));
const int diff = max(nums[i] - nums[i - 1], 0);
for (int j = diff; j <= nums[i]; ++j) {
new_dp[j] = ((j - 1 >= 0 ? new_dp[j - 1] : 0) + dp[j - diff]) % MOD;
}
dp = move(new_dp);
}
return accumulate(cbegin(dp), cend(dp), 0, [&](const auto& accu, const auto& x) {
return (accu + x) % MOD;
});
}
};
// Time: O(n * r^2), r = max(nums)
// Space: O(r)
// dp
class Solution3 {
public:
int countOfPairs(vector<int>& nums) {
static const int MOD = 1e9 + 7;
vector<int> dp(ranges::max(nums) + 1); // dp[j]: numbers of arr1, which is of length i+1 and arr1[i] is j
for (int i = 0; i <= nums[0]; ++i) {
dp[i] = 1;
}
for (int i = 1; i < size(nums); ++i) {
// arr1[i-1] <= arr1[i]
// => arr1[i]-arr1[i-1] >= 0 (1)
//
// arr2[i-1] >= arr2[i]
// => nums[i-1]-arr1[i-1] >= nums[i]-arr1[i]
// => arr1[i]-arr1[i-1] >= nums[i]-nums[i-1] (2)
//
// (1)+(2): arr1[i]-arr1[i-1] >= max(nums[i]-nums[i-1], 0)
vector<int> new_dp(size(dp));
const int diff = max(nums[i] - nums[i - 1], 0);
for (int j = diff; j <= nums[i]; ++j) {
for (int k = diff; k <= j; ++k) {
new_dp[j] = (new_dp[j]+ dp[k - diff]) % MOD;
}
}
dp = move(new_dp);
}
return accumulate(cbegin(dp), cend(dp), 0, [&](const auto& accu, const auto& x) {
return (accu + x) % MOD;
});
}
};