Skip to content

Instantly share code, notes, and snippets.

@donjar
Created October 17, 2017 10:24
Show Gist options
  • Save donjar/7fc0209feb77cbaf97df9635090f1b11 to your computer and use it in GitHub Desktop.
Save donjar/7fc0209feb77cbaf97df9635090f1b11 to your computer and use it in GitHub Desktop.
class Solution {
int[][] solutionTable;
public int kInversePairs(int n, int k) {
solutionTable = new int[n + 1][k + 1];
return dp(n, k);
}
private int dp(int n, int k) {
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= k; j++) {
if (i == 1) {
solutionTable[i][j] = (j == 0) ? 1 : 0;
} else {
for (int m = 0; m < i; m++) {
if (m > j) {
break;
}
solutionTable[i][j] += solutionTable[i - 1][j - m];
}
}
}
}
return solutionTable[n][k];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment