forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_629.java
36 lines (33 loc) · 1.14 KB
/
_629.java
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
package com.fishercoder.solutions;
public class _629 {
public static class Solution1 {
/**
* reference: https://leetcode.com/articles/k-inverse-pairs-array/#approach-5-another-optimized-dynamic-programming-approachaccepted
* and
* https://discuss.leetcode.com/topic/93815/java-dp-o-nk-solution
*/
public int kInversePairs(int n, int k) {
int mod = 1000000007;
if (k > n * (n - 1) / 2 || k < 0) {
return 0;
}
if (k == 0 || k == n * (n - 1) / 2) {
return 1;
}
long[][] dp = new long[n + 1][k + 1];
dp[2][0] = 1;
dp[2][1] = 1;
for (int i = 3; i <= n; i++) {
dp[i][0] = 1;
for (int j = 1; j <= Math.min(k, i * (i - 1) / 2); j++) {
dp[i][j] = dp[i][j - 1] + dp[i - 1][j];
if (j >= i) {
dp[i][j] -= dp[i - 1][j - i];
}
dp[i][j] = (dp[i][j] + mod) % mod;
}
}
return (int) dp[n][k];
}
}
}