题解 | 打家劫舍(一)

打家劫舍(一)

https://www.nowcoder.com/practice/c5fbf7325fbd4c0ea3d0c3ea6bc6cc79

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

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

全部评论

相关推荐

03-29 14:19
门头沟学院 Java
你背过凌晨4点的八股文么:加油同学,人生的容错率很高,只是一个暑期罢了,后面还有很多机会!
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务