Skip to content

Instantly share code, notes, and snippets.

@shixiaoyu
Created June 14, 2020 18:01
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 shixiaoyu/72ded54dd04f50e12b7359d1183ef7c8 to your computer and use it in GitHub Desktop.
Save shixiaoyu/72ded54dd04f50e12b7359d1183ef7c8 to your computer and use it in GitHub Desktop.
public int rob(int[] num) {
if (num == null || num.length == 0) {
return 0;
}
int n = num.length;
int[] lookup = new int[n + 1]; // DP array size normally larger than 1
lookup[0] = 0;
lookup[1] = num[0];
for (int i = 2; i <= n; i++) {
lookup[i] = Math.max(lookup[i - 1], lookup[i - 2] + num[i - 1]);
}
return lookup[n];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment