fast and O(1) space solution


  • 0
    R
     int rob(vector<int>& nums) {
        if(nums.size()==0)
            return 0;
        if(nums.size()==1)
            return nums[0];
        int a = nums[0];
        int b = max(nums[1],nums[0]);
        for(int i=2;i<nums.size();i++){
            int temp = b;
            b = max(b,a+nums[i]);
            a = temp;
        }
        return max(b,a);
        
        
    }

Log in to reply
 

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