LeetCode:House Robber系列

本文详细解析了LeetCode上的三个经典动态规划问题:House Robber I、II 和 III。介绍了如何通过不同策略来解决这些抢劫房屋的问题,包括一维数组到二叉树的递归解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

198. House Robber


已经有说明了,这篇第九个题。
http://blog.youkuaiyun.com/bestzem/article/details/51884230


213. House Robber II


Note: This is an extension of House Robber.

After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.


绝大部分继承自上一题,主要思路:由于前后数组相连,结果关系包含肯定是要么有头,要么有尾,要么两者都没有。此时我们可以计算一次在没有头的情况最大能得到,计算一次在没有尾的时候最大能得到多少,两次取最大即可。


class Solution {
public:
    int rob(vector<int>& nums) {
        int sz=nums.size();
        if(nums.empty())    return 0;
        if(sz==1)   return nums[0];
        if(sz==2)   return max(nums[0],nums[1]);
        vector<int> maxRob(sz,0);
        maxRob[1]=nums[1];
        maxRob[2]=nums[2];
        for(int i=3;i<sz;++i)//get without nums[0]
        {
            maxRob[i]=nums[i]+*(max_element(maxRob.cbegin()+1,maxRob.cbegin()+i-1));//[b,c)
        }
        int res=*(max_element(maxRob.cbegin()+1,maxRob.cend()));
        maxRob[0]=nums[0];
        maxRob[1]=nums[1];
        for(int i=2;i<sz-1;++i)//get without nums[sz-1]
        {
            maxRob[i]=nums[i]+*(max_element(maxRob.cbegin(),maxRob.cbegin()+i-1));//[b,c)
        }
        return  max(res,*(max_element(maxRob.cbegin(),maxRob.cend()-1)));
    }
};

337. House Robber III


The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the “root.” Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that “all houses in this place forms a binary tree”. It will automatically contact the police if two directly-linked houses were broken into on the same night.

Determine the maximum amount of money the thief can rob tonight without alerting the police.

Example 1:
3
/ \
2 3
\ \
3 1
Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
Example 2:
3
/ \
4 5
/ \ \
1 3 1
Maximum amount of money the thief can rob = 4 + 5 = 9.


DFS解决问题,其实思路和上一题很相似,我们可以求出两种情况,第一个是父节点与孩纸节点的子树和求出最大,第二是两个孩子节点和求出最大,两者选出最大就可以了。
这一题我跑偏了,看了别人的写出来的。

第一种(注释掉的):多次进入递归,使用map防止重复遍历。

第二种:两次进入递归,用数组存储每次递归进入返回的值。

Reference:
https://discuss.leetcode.com/topic/39834/step-by-step-tackling-of-the-problem

第二种方式有很多变形,比较简单的如

https://discuss.leetcode.com/topic/40847/simple-c-solution

此题中的来说让人深思,原来二叉树遍历还可以玩出这么多花样。还要努力学习才行。


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    // int robMap(TreeNode* root,map<TreeNode*,int> &mti)
    // {
    //     if(!root)   return 0;
    //     //遇到已经遍历过的子树,直接返回
    //     if(mti.find(root)!=mti.end())   return mti[root];
    //     int res{0};
    //     if(root->left)
    //     {
    //         res+=(robMap(root->left->left,mti)+robMap(root->left->right,mti));
    //     }
    //     if(root->right)
    //     {
    //         res+=(robMap(root->right->left,mti)+robMap(root->right->right,mti));
    //     }
    //     res=max(res+root->val,robMap(root->left,mti)+robMap(root->right,mti));
    //     mti[root]=res;
    //     return res;
    // }

    array<int,2> robSum(TreeNode *root)
    {
        array<int,2> res{};
        if(!root)    return res;
        array<int,2> left=robSum(root->left);
        array<int,2> right=robSum(root->right);
        res[0]=max(left[0],left[1])+max(right[0],right[1]);//此时,res[1]远离父节点
        res[1]=root->val+left[0]+right[0];//此时,res[1]与父节点保持联系
        return res;
    }

    int rob(TreeNode* root) {
        array<int,2> res=robSum(root);
        return max(res[0],res[1]);
        // map<TreeNode*,int> mti;
        // return robMap(root,mti);
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值