Skip to content

Instantly share code, notes, and snippets.

@blancosj
Created March 27, 2019 20:45
Show Gist options
  • Save blancosj/8ed8ad857264dc3af0aad3fa9a0a4be8 to your computer and use it in GitHub Desktop.
Save blancosj/8ed8ad857264dc3af0aad3fa9a0a4be8 to your computer and use it in GitHub Desktop.
public static int partitionKDim(int[] A, int k, int target) {
int[][][] mem = new int[k + 1][A.length + 1][target + 1];
for (int t = 0; t < k + 1; t++) {
for (int i = 0; i < A.length + 1; i++) {
mem[t][i][0] = 1;
}
}
for (int t = 1; t < k + 1; t++) {
for (int i = 1; i < A.length + 1; i++) {
for (int j = 1; j < target + 1; j++) {
mem[t][i][j] = mem[t][i - 1][j];
int p = j - A[i - 1];
if (p >= 0) {
mem[t][i][j] = mem[t][i][j] + mem[t - 1][i - 1][p];
}
}
}
}
for (int t = 0; t < k + 1; t++) {
printSquare(mem[t]);
}
return mem[k][A.length][target];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment