LeetCode Solutions
629. K Inverse Pairs Array
Time: $O(nk)$ Space: $O(nk)$
class Solution {
public:
int kInversePairs(int n, int k) {
constexpr int kMod = 1'000'000'007;
// dp[i][j] := # of permutations of numbers 1..i with j inverse pairs
vector<vector<int>> dp(n + 1, vector<int>(k + 1));
// If there's no inverse pair, the permutation is unique "123..i"
for (int i = 0; i <= n; ++i)
dp[i][0] = 1;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= k; ++j) {
dp[i][j] = (dp[i][j - 1] + dp[i - 1][j]) % kMod;
if (j - i >= 0)
dp[i][j] = (dp[i][j] - dp[i - 1][j - i] + kMod) % kMod;
}
return dp[n][k];
}
};
class Solution {
public int kInversePairs(int n, int k) {
final int kMod = 1_000_000_007;
// dp[i][j] := # of permutations of numbers 1..i with j inverse pairs
int[][] dp = new int[n + 1][k + 1];
// If there's no inverse pair, the permutation is unique "123..i"
for (int i = 0; i <= n; ++i)
dp[i][0] = 1;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= k; ++j) {
dp[i][j] = (dp[i][j - 1] + dp[i - 1][j]) % kMod;
if (j - i >= 0)
dp[i][j] = (dp[i][j] - dp[i - 1][j - i] + kMod) % kMod;
}
return dp[n][k];
}
}
class Solution:
def kInversePairs(self, n: int, k: int) -> int:
kMod = 1_000_000_007
# dp[i][j] := # Of permutations of numbers 1..i with j inverse pairs
dp = [[0] * (k + 1) for _ in range(n + 1)]
# If there's no inverse pair, the permutation is unique '123..i'
for i in range(n + 1):
dp[i][0] = 1
for i in range(1, n + 1):
for j in range(1, k + 1):
dp[i][j] = (dp[i][j - 1] + dp[i - 1][j]) % kMod
if j - i >= 0:
dp[i][j] = (dp[i][j] - dp[i - 1][j - i] + kMod) % kMod
return dp[n][k]