Simple, DP, Java solution


  • 0
    B
    public int rob(int[] num) {
    
        if (num == null || num.length == 0) {
            return 0;
        }
        int a = 0;
        int b = 0;
        int tmp;
        for (int i = 0; i < num.length; i++) {
            tmp = b;
            b = Math.max(a + num[i], b);
            a = tmp;
        }
        return b;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.