Created
October 17, 2017 10:24
-
-
Save donjar/7fc0209feb77cbaf97df9635090f1b11 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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