Created
June 14, 2020 18:01
-
-
Save shixiaoyu/72ded54dd04f50e12b7359d1183ef7c8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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