【LeetCode】打家劫舍||

打家劫舍||

链接: 打家劫舍||

在做这个题之前,建议大家做一下这个链接: 按摩师

我的博客里也有这个题的讲解,名字是按摩师

题目描述

算法分析

编程代码

cpp 复制代码
class Solution {
public:
    int maxrob(vector<int>nums,int left,int right){
        if(left > right) return 0;
        //int n = nums.size();
        vector<int>f(right+1);
        auto g = f;
        f[left] = nums[left];
        for(int i = left+1;i<=right;++i)
        {
            f[i] = g[i-1] + nums[i];
            g[i] = max(f[i-1],g[i-1]);
        }
        return max(f[right],g[right]);
    }

    int rob(vector<int>& nums) {

        //int n = nums.size();
        return max(nums[0] + maxrob(nums,2,nums.size()-2)
                    ,maxrob(nums,1,nums.size()-1));
    }
};
相关推荐
文丑颜不良啊20 小时前
LeetCode2221~2240题目解析
java·leetcode
Navigator_Z21 小时前
LeetCode //C - 1317. Convert Integer to the Sum of Two No-Zero Integers
c语言·算法·leetcode
文丑颜不良啊1 天前
LeetCode2161~2180题目解析
java·leetcode
文丑颜不良啊1 天前
LeetCode2141~2160题目解析
java·leetcode
Dream it possible!2 天前
LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等)
算法·leetcode·面试·动态规划
心抵鹊2 天前
算法之双指针系列(一)
算法·leetcode
Pniubi2 天前
力扣560.和为k的子数组
算法·leetcode·职场和发展
Pniubi2 天前
力扣438.找到字符串中所有字母异位词
算法·leetcode·职场和发展
文丑颜不良啊2 天前
LeetCode2081~2100题目解析
java·leetcode
文丑颜不良啊2 天前
LeetCode2061~2080题目解析
java·leetcode