-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
count-the-number-of-inversions.cpp
120 lines (111 loc) · 4.05 KB
/
count-the-number-of-inversions.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
// Time: O(n * k), k = max(cnt for _, cnt in requirements)
// Space: O(n + k)
// knapsack dp, combinatorics, sliding window, two pointers
class Solution {
public:
int numberOfPermutations(int n, vector<vector<int>>& requirements) {
static const int MOD = 1e9 + 7;
const auto& addmod = [&](const auto& a, const auto& b) {
return (a + b) % MOD;
};
const auto& submod = [&](const auto& a, const auto& b) {
return ((a - b) % MOD + MOD) % MOD;
};
vector<int> lookup(n, -1);
for (const auto& r : requirements) {
lookup[r[0]] = r[1];
}
vector<int> dp = {1};
for (int i = 0, prev = 0; i < n; ++i) {
if (lookup[i] != -1) {
dp = { accumulate(cbegin(dp) + max((lookup[i] - i) - prev, 0), cbegin(dp) + min(((lookup[i] + 1) - prev), static_cast<int>(size(dp))), 0, addmod) };
prev = lookup[i];
continue;
}
vector<int> new_dp(min(static_cast<int>(size(dp)) + ((i + 1) - 1), (lookup.back() + 1) - prev));
for (int j = 0; j < size(new_dp); ++j) {
new_dp[j] = j < size(dp) ? dp[j] : 0;
if (j - 1 >= 0) {
new_dp[j] = addmod(new_dp[j], new_dp[j - 1]);
}
if (j - (i + 1) >= 0) {
new_dp[j] = submod(new_dp[j], dp[j - (i + 1)]);
}
}
dp = move(new_dp);
}
return dp.back();
}
};
// Time: O(n * k), k = max(cnt for _, cnt in requirements)
// Space: O(n + k)
// knapsack dp, combinatorics, sliding window, two pointers
class Solution2 {
public:
int numberOfPermutations(int n, vector<vector<int>>& requirements) {
static const int MOD = 1e9 + 7;
const auto& addmod = [&](const auto& a, const auto& b) {
return (a + b) % MOD;
};
const auto& submod = [&](const auto& a, const auto& b) {
return ((a - b) % MOD + MOD) % MOD;
};
vector<int> lookup(n, -1);
for (const auto& r : requirements) {
lookup[r[0]] = r[1];
}
vector<int> dp(lookup.back() + 1);
dp[0] = 1;
for (int i = 0; i < n; ++i) {
vector<int> new_dp(size(dp));
if (lookup[i] != -1) { // optimized
new_dp[lookup[i]] = accumulate(cbegin(dp) + max(lookup[i] - i, 0), cbegin(dp) + (lookup[i] + 1), 0, addmod);
} else {
for (int j = 0; j < size(dp); ++j) {
new_dp[j] = dp[j];
if (j - 1 >= 0) {
new_dp[j] = addmod(new_dp[j], new_dp[j - 1]);
}
if (j - (i + 1) >= 0) {
new_dp[j] = submod(new_dp[j], dp[j - (i + 1)]);
}
}
}
dp = move(new_dp);
}
return dp.back();
}
};
// Time: O(n * k), k = max(cnt for _, cnt in requirements)
// Space: O(n + k)
// knapsack dp, combinatorics, sliding window, two pointers
class Solution3 {
public:
int numberOfPermutations(int n, vector<vector<int>>& requirements) {
static const int MOD = 1e9 + 7;
const auto& addmod = [&](const auto& a, const auto& b) {
return (a + b) % MOD;
};
const auto& submod = [&](const auto& a, const auto& b) {
return ((a - b) % MOD + MOD) % MOD;
};
vector<int> lookup(n, -1);
for (const auto& r : requirements) {
lookup[r[0]] = r[1];
}
vector<int> dp(lookup.back() + 1);
dp[0] = 1;
for (int i = 0; i < n; ++i) {
vector<int> new_dp(size(dp));
for (int j = 0, curr = 0; j < size(dp); ++j) {
curr = addmod(curr, dp[j]);
if (j - (i + 1) >= 0) {
curr = submod(curr, dp[j - (i + 1)]);
}
new_dp[j] = lookup[i] == -1 || lookup[i] == j ? curr : 0;
}
dp = move(new_dp);
}
return dp.back();
}
};