My AC 0ms C++ Solution, simple and easy (only 5 lines)


  • -1
    C
    class Solution {
    public:
        int rob(vector<int>& nums) {
    		if(nums.size() < 1) return 0;
    		nums.insert(nums.end(), 3, 0);  //Insert 3 zero to solve the problem of size < 4
    		for(int i=nums.size()-4;i>=0;--i) 
    		  nums[i] = nums[i] + max(nums[i+2], nums[i+3]);
    		return max(nums[0], nums[1]);
    	}
    };

Log in to reply
 

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