【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));
    }
};
相关推荐
alphaTao1 小时前
LeetCode 每日一题 2026/3/23-2026/3/29
服务器·windows·leetcode
不光头强2 小时前
力扣78子集题解
算法·leetcode·深度优先
Magic--2 小时前
经典概率题:飞机座位分配问题(LeetCode 1227)超详细解析
算法·leetcode·职场和发展
We་ct3 小时前
LeetCode 4. 寻找两个正序数组的中位数:二分优化思路详解
前端·数据结构·算法·leetcode·typescript·二分
滴滴答滴答答5 小时前
LeetCode Hot100 之 41 缺失的第一个正数
算法·leetcode·职场和发展
Sakinol#5 小时前
Leetcode Hot 100 ——多维动态规划
算法·leetcode·动态规划
xsyaaaan5 小时前
leetcode-hot100-二叉树
数据结构·leetcode
_日拱一卒5 小时前
LeetCode:无重复字符的最长字串
算法·leetcode·职场和发展
迷海5 小时前
力扣原题《有效的数独游戏》,纯手搓,已验证
算法·leetcode·游戏