【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));
    }
};
相关推荐
hn小菜鸡10 小时前
LeetCode 377.组合总和IV
数据结构·算法·leetcode
亮亮爱刷题10 天前
飞往大厂梦之算法提升-7
数据结构·算法·leetcode·动态规划
zmuy10 天前
124. 二叉树中的最大路径和
数据结构·算法·leetcode
chao_78910 天前
滑动窗口题解——找到字符串中所有字母异位词【LeetCode】
数据结构·算法·leetcode
Alfred king10 天前
面试150跳跃游戏
python·leetcode·游戏·贪心算法
呆呆的小鳄鱼10 天前
leetcode:746. 使用最小花费爬楼梯
算法·leetcode·职场和发展
YuTaoShao10 天前
【LeetCode 热题 100】42. 接雨水——(解法一)前后缀分解
java·算法·leetcode·职场和发展
YuforiaCode10 天前
(LeetCode 面试经典 150 题) 27.移除元素
算法·leetcode·面试
呆呆的小鳄鱼10 天前
leetcode:98. 验证二叉搜索树
算法·leetcode·职场和发展
alphaTao11 天前
LeetCode 每日一题 2025/6/16-2025/6/22
算法·leetcode