Simple JS O(1) space and O(n) time solution


  • 4
    Y
    var rob = function(nums) {
        
        var vMissed = 0;
        var vChosen = 0;
        var temp;
        
        for(var i = 0; i < nums.length; i++){
            temp = Math.max(vMissed, vChosen);
                
            vChosen = vMissed + nums[i];
            vMissed = temp;
        }
    
        return Math.max(vMissed, vChosen);
    };

Log in to reply
 

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