c++ dp solution (O(n))


  • 0
    int rob(vector<int>& nums) {
        int res = 0;
        for (int i = 0; i < nums.size(); ++i) {
            res = max(res, nums[i] + ((i < 2) ? 0 : nums[i - 2]));
            nums[i] = res;   
        }
        return res;
    }
    

Log in to reply
 

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