-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkinverse.java
28 lines (23 loc) · 867 Bytes
/
kinverse.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
public class kinverse {
public int kInversePairs(int n, int k) {
int MOD = 100000007;
int[][] dp = new int[n + 1][k + 1];
// columns are 'n' and Rows are 'k'
// We move : n=1 > Test it for all possible k then n=2 > Test it for all value
// of k
for (int c = 1; c < n + 1; c++) {
for (int r = 0; r < k + 1; r++) {
if (r == 0)
dp[0][c] = 1;
else {
// consider exactly 'coloumn-number' elements from previous column to find the
// value of dp[r][c]
for (int past = 0; past <=Math.min(r,c-1); past++) {
dp[r][c] =( dp[r][c] +dp[r-past][c - 1])% MOD;
}
}
}
}
return dp[n][k];
}
}