C++, easy to understand


  • -1
    C
    class Solution {
    public:
        int rob(vector<int>& nums) {
            if(nums.empty()) return 0;
            else if(nums.size() == 1) return nums[0];
            else if(nums.size() == 2) return max(nums[0],nums[1]);
            else if(nums.size() == 3) return max(nums[0] + nums[2],nums[1]);
            
            nums[2] += nums[0];
            
            for(int i = 3;i < nums.size();i++){
                nums[i] += max(nums[i - 2],nums[i - 3]);
            }
            
            return max(nums[nums.size() - 2],nums[nums.size() - 1]);
        }
    };

Log in to reply
 

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