Skip to content

Instantly share code, notes, and snippets.

@resendes
Created December 9, 2019 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save resendes/593931d135f089173c0cd9d62b4f3037 to your computer and use it in GitHub Desktop.
Save resendes/593931d135f089173c0cd9d62b4f3037 to your computer and use it in GitHub Desktop.
public int subsetSumDinamycProgramming() {
int[] x = {2,3,5,7,8,10,15};
int t = 23;
int n = x.length;
int[][] s = new int[n+1][t+1];
s[n][0] = 1;
for (int j = 1; j <= t; j++) {
s[n][j] = 0;
}
for (int i = n - 1; i >= 0; i--) {
s[i][0] = 1;
for (int j = 1; j <= x[i] - 1; j++) {
s[i][j] = s[i+1][j];
}
for (int j = x[i]; j <= t; j++) {
int aux = s[i + 1][j];
if (aux == 1) {
s[i][j] = aux;
} else {
s[i][j] = s[i + 1][j - x[i]];
}
}
}
return s[0][t];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment