House robbing question: 198Leetcode

mail@pastecode.io avatar
unknown
c_cpp
3 years ago
405 B
2
Indexable
Never
int rob(vector<int>& nums) {
        if(nums.size() == 1)
            return nums[0];
        if(nums.size() == 2){
            return max(nums[0], nums[1]);
        }
        nums[2] = max(nums[1], nums[0]+nums[2]);
        for(int i = 3; i < nums.size(); i++){
            nums[i] = max(nums[i-1], max(nums[i]+nums[i-3],nums[i]+nums[i-2]));
        }
        return nums[nums.size()-1];
    }