package leetcode;

/**
 * https://leetcode.com/problems/house-robber-ii/
 */
public class Problem213 {
    public int rob(int[] nums) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem213 prob = new Problem213();
    }
}

@@ -12,16 +12,65 @@
 tcode;%0A%0A
+import java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 /**%0A * h
@@ -190,129 +190,1816 @@
     
-// TODO%0A        return 0;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem213 prob = new Problem213();
+int max = 0;%0A        if (nums.length %3C= 3) %7B%0A            for (int num : nums) %7B%0A                max = Math.max(max, num);%0A            %7D%0A            return max;%0A        %7D%0A        Map%3CInteger, Integer%3E memo = new HashMap%3C%3E();%0A        int a = rob(nums, 0, 0, memo);%0A        int b = rob(nums, 1, 1, memo);%0A        int lastVal = nums%5Bnums.length-1%5D;%0A        if (nums.length-3 %25 2 == 0 %7C%7C nums.length-3 %25 3 == 0) %7B%0A            max = Math.max(a+lastVal, b);%0A        %7D else %7B%0A            max = Math.max(a, b+lastVal);%0A        %7D%0A        return max;%0A    %7D%0A%0A    private int rob(int%5B%5D nums, int begIdx, int idx, Map%3CInteger, Integer%3E memo) %7B%0A        if (memo.containsKey(idx)) %7B%0A            return memo.get(idx);%0A        %7D%0A        if (idx %3E= nums.length-1) %7B%0A            return 0;%0A        %7D%0A        int a = rob(nums, begIdx, idx+2, memo);%0A        int b = rob(nums, begIdx, idx+3, memo);%0A        int val = nums%5Bidx%5D;%0A        int result = Math.max(a+val, b+val);%0A        memo.put(idx, result);%0A        return result;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem213 prob = new Problem213();%0A//        System.out.println(prob.rob(new int%5B%5D%7B2, 1%7D)); // 2%0A//        System.out.println(prob.rob(new int%5B%5D%7B1, 2%7D)); // 2%0A//        System.out.println(prob.rob(new int%5B%5D%7B1, 1, 1%7D)); // 1%0A//        System.out.println(prob.rob(new int%5B%5D%7B1%7D)); // 1%0A//        System.out.println(prob.rob(new int%5B%5D%7B6, 6, 4, 8, 4, 3, 3, 10%7D)); // 27%0A//        System.out.println(prob.rob(new int%5B%5D%7B2, 1, 1, 2%7D)); // 3%0A//        System.out.println(prob.rob(new int%5B%5D%7B2, 1, 1, 1%7D)); // 3%0A//        System.out.println(prob.rob(new int%5B%5D%7B2, 1, 1, 4%7D)); // 5%0A//        System.out.println(prob.rob(new int%5B%5D%7B1, 1, 3, 6, 7, 10, 7, 1, 8, 5, 9, 1, 6, 4, 3%7D)); // 43%0A        System.out.println(prob.rob(new int%5B%5D%7B1, 3, 1, 3, 100%7D)); // 103
 %0A   

Update problem 213