For an integer array nums
, an inverse pair is a pair of integers [i, j]
where 0 <= i < j < nums.length
and nums[i] > nums[j]
.
Given two integers n and k, return the number of different arrays consist of numbers from 1
to n
such that there are exactly k
inverse pairs. Since the answer can be huge, return it modulo 109 + 7
.
Example 1:
Input: n = 3, k = 0 Output: 1 Explanation: Only the array [1,2,3] which consists of numbers from 1 to 3 has exactly 0 inverse pairs.
Example 2:
Input: n = 3, k = 1 Output: 2 Explanation: The array [1,3,2] and [2,1,3] have exactly 1 inverse pair.
Constraints:
1 <= n <= 1000
0 <= k <= 1000
class Solution:
def kInversePairs(self, n: int, k: int) -> int:
mod = 1000000007
dp, pre = [0] * (k + 1), [0] * (k + 2)
for i in range(1, n + 1):
dp[0] = 1
# dp[i][j] = dp[i - 1][j - (i - 1)] + ... + dp[i - 1][j]
for j in range(1, k + 1):
dp[j] = (pre[j + 1] - pre[max(0, j - i + 1)] + mod) % mod
for j in range(1, k + 2):
pre[j] = (pre[j - 1] + dp[j - 1]) % mod
return dp[k]
dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1] + dp[i - 1][j - 2] + ... + dp[i - 1][j - (i - 1)]
①
dp[i][j - 1] = dp[i - 1][j - 1] + dp[i - 1][j - 2] + ... + dp[i - 1][j - (i - 1)] + dp[i - 1][j - i]
②
① - ②, dp[i][j] = dp[i][j - 1] + dp[i - 1][j] - dp[i - 1][j - i]
class Solution:
def kInversePairs(self, n: int, k: int) -> int:
N, MOD = 1010, int(1e9) + 7
dp = [[0] * N for _ in range(N)]
dp[1][0] = 1
for i in range(2, n + 1):
dp[i][0] = 1
for j in range(1, k + 1):
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
if j >= i:
dp[i][j] -= dp[i - 1][j - i]
dp[i][j] %= MOD
return dp[n][k]
class Solution:
def kInversePairs(self, n: int, k: int) -> int:
N, MOD = 1010, int(1e9) + 7
dp = [0] * N
dp[0] = 1
for i in range(2, n + 1):
t = dp.copy()
for j in range(1, k + 1):
dp[j] = t[j] + dp[j - 1]
if j >= i:
dp[j] -= t[j - i]
dp[j] %= MOD
return dp[k]
class Solution {
private static final int MOD = 1000000007;
public int kInversePairs(int n, int k) {
int[] dp = new int[k + 1];
int[] pre = new int[k + 2];
for (int i = 1; i <= n; i++) {
dp[0] = 1;
// dp[i][j] = dp[i - 1][j - (i - 1)] + ... + dp[i - 1][j]
for (int j = 1; j <= k; j++) {
dp[j] = (pre[j + 1] - pre[Math.max(0, j - i + 1)] + MOD) % MOD;
}
for (int j = 1; j <= k + 1; j++) {
pre[j] = (pre[j - 1] + dp[j - 1]) % MOD;
}
}
return dp[k];
}
}
class Solution {
public int kInversePairs(int n, int k) {
int N = 1010, MOD = (int) (1e9 + 7);
int[][] dp = new int[N][N];
dp[1][0] = 1;
for (int i = 2; i < n + 1; ++i) {
dp[i][0] = 1;
for (int j = 1; j < k + 1; ++j) {
dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % MOD;
if (j >= i) {
dp[i][j] = (dp[i][j] - dp[i - 1][j - i] + MOD) % MOD;
}
}
}
return dp[n][k];
}
}
const mod int = 1e9 + 7
func kInversePairs(n int, k int) int {
dp := make([]int, k+1)
pre := make([]int, k+2)
for i := 1; i <= n; i++ {
dp[0] = 1
// dp[i][j] = dp[i - 1][j - (i - 1)] + ... + dp[i - 1][j]
for j := 1; j <= k; j++ {
dp[j] = (pre[j+1] - pre[max(0, j-i+1)] + mod) % mod
}
for j := 1; j <= k+1; j++ {
pre[j] = (pre[j-1] + dp[j-1]) % mod
}
}
return dp[k]
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
class Solution {
private:
static constexpr int MOD = 1e9 + 7;
public:
int kInversePairs(int n, int k) {
vector<int> dp(k + 1), pre(k + 2, 0);
for (int i = 1; i <= n; ++i) {
dp[0] = 1;
// dp[i][j] = dp[i - 1][j - (i - 1)] + ... + dp[i - 1][j]
for (int j = 1; j <= k; ++j) {
dp[j] = (pre[j + 1] - pre[max(0, j - i + 1)] + MOD) % MOD;
}
for (int j = 1; j <= k + 1; ++j) {
pre[j] = (pre[j - 1] + dp[j - 1]) % MOD;
}
}
return dp[k];
}
};