打家劫舍(动态规划+滚动数组+取模运算优化)
public static void main(String[] args) { int[] a = {2,1,1,3}; int[] b = {2,7,9,3,1}; System.out.println(rob(a)); System.out.println(rob(b)); } public static int rob(int[] nums) { int len = nums.length; if(len == 0) return 0; int[] dp = new int[len + 1]; dp[0] = 0; dp[1] = nums[0]; for(int i = 2; i <= len; i++) { dp[i] = Math.max(dp[i-1], dp[i-2] + nums[i-1]); } return dp[len]; }