Java dp solution


  • 0
    W
    public int rob(int[] nums) {
        //dp
        int len = nums.length;
        if(len == 0) return 0;
        else if(len == 1) return nums[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];
    }

Log in to reply
 

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