My Java Solution


  • 0
    C
    public int rob(int[] nums) {
        if(nums.length == 0) return 0;
        
        int s0 = nums[0];           //robbing
        int s1 = 0;                 //not robbing
        for(int i = 1; i < nums.length; i++) {
            int t = s0;
            s0 = s1 + nums[i];
            s1 = Math.max(t, s1);
        }
        return Math.max(s0, s1);
    }

  • 0
    X

    public int rob(int[] nums) {
    if(nums.length == 0) return 0;

    int s0 = nums[0];           //robbing
    int s1 = 0;                 //not robbing
    for(int i = 1; i < nums.length; i++) {
        int t = s0;
        s0 = s1 + nums[i];
        s1 = Math.max(t, s1);
    }
    return Math.max(s0, s1);
    

    }


Log in to reply
 

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