【打卡】牛客网:BM78 打家劫舍(一)

模板的: 

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param nums int整型vector 
     * @return int整型
     */
    int rob(vector& nums) {
        // write code here
        int n = nums.size();
        vector dp(n+1);
        dp[1] = nums[0];

        for(int i = 2; i <= n; i++){
            dp[i] = max(dp[i-1], dp[i-2]+nums[i-1]);
        }

        return dp[n];
    }
};

你可能感兴趣的:(算法,leetcode,数据结构)