题解 | 打家劫舍(一)

打家劫舍(一)

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]);
    }
};

全部评论

相关推荐

孙艹肘:校招不给三方直接让实习我都去了,,主打一个在学校呆着也是闲着,不如出来实习一下
点赞 评论 收藏
分享
给🐭🐭个面试机会...:我擦seed✌🏻
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务