Leetcode 198. 打家劫舍

tou[k]=butou[k-1]+nums[k]
butou[k]=max(butou[k-1],tou[k-1])
压缩数组,只要两个变量就成

class Solution {
public:
    int rob(vector<int>& nums) {
        int tou=0,butou=0,t;
        for(auto& x:nums)
            t=butou+x,butou=max(tou,butou),tou=t;
        return max(tou,butou);
    }
};

猜你喜欢

转载自blog.csdn.net/bendaai/article/details/81144871