Java Commented AC solution may help you


  • 0
    U
    public class Solution {// at each house we have two state. rob or not.
    public int rob(int[] nums) {
        if(nums.length==0)
            return 0;
        int robY=nums[0];
        int robN=0;
        int temp;
        for(int i=1;i<nums.length;i++){
            temp=robY;
            robY=robN+nums[i];
            robN=Math.max(temp,robN);// if we chose not rob this house we stick with the maxium of previous robY and robN
        }
        return Math.max(robY,robN);
    }
    

    }


Log in to reply
 

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