Easy Java DP Solution


  • 0
    public int rob(int[] nums) {
        if(nums.length==0)
            return 0;
        int[] dp = new int[nums.length];
        for(int i=0; i<nums.length; i++) {
            if(i==0)
                dp[0] = nums[0];
            else if(i==1)
                dp[1] = Math.max(nums[0],nums[1]);
            else
                dp[i] = Math.max(dp[i-2]+nums[i],dp[i-1]);
        }
        return dp[nums.length-1];
    }

Log in to reply
 

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