-
Notifications
You must be signed in to change notification settings - Fork 0
/
629KInversePairsArray.java
35 lines (34 loc) · 1.09 KB
/
629KInversePairsArray.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
public class Solution {
public int kInversePairs(int n, int k) {
long[][] dp = new long[n][k+1];
dp[0][0]=1;
for(int i=1;i<n;i++){
for(int j=0;j<=k;j++){
if(j>0){
dp[i][j]=dp[i][j-1]+dp[i-1][j];
dp[i][j]=j-i>0?(dp[i][j]-dp[i-1][j-i-1]):dp[i][j];
}
else{
for(int m=j;m>=0&&m>=(j-i);m--){
dp[i][j]+=dp[i-1][m];
}
}
dp[i][j]=dp[i][j]>0?dp[i][j]%1000000007:(dp[i][j]+1000000007)%1000000007;
}
}
return (int)dp[n-1][k];
}
public int kInversePairs(int n, int k) {
long[][] dp = new long[n][k+1];
dp[0][0]=1;//mean sum of arrays that consist of 1 with 0 inverse pairs
for(int i=1;i<n;i++){
for(int j=0;j<=k;j++){
for(int m=j;m>=0&&m>=(j-i);m--){
dp[i][j]+=dp[i-1][m];
}
dp[i][j]=dp[i][j]%1000000007;
}
}
return (int)dp[n-1][k];
}
}