[leetcode] House Robber [i ii iii]

本文探讨了一种经典的算法问题——房屋抢劫问题。该问题要求在不触动相邻房屋警报系统的前提下,计算出能从一系列房屋中抢劫到的最大金额。文章详细介绍了三种不同情况下的解决方案:直线排列的房屋、环形排列的房屋以及构成二叉树结构的房屋。

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

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

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.

思想:nums[i]表示偷取i时,能获得的最大收益,那么必然是先偷[0,1,....i-2]中的一个,再偷num[i]

premax表示[0,1,....,i-2]中的能偷取的最大利益。res 表示能获得的最大收益。

举例:[1,8,2,1,100]

[1,8,3,9,108]

class Solution {
public:
    int rob(vector<int>& nums) {
        if(nums.size()==0) return 0;
        else if(nums.size()==1) return nums[0];
        else if(nums.size()==2) return max(nums[0],nums[1]);
        int premax=nums[0];
        int res=max(nums[0],nums[1]);
        for (int i = 2; i < nums.size(); ++i)
        {
            if(nums[i-2]>premax) premax=nums[i-2];
            nums[i]+=premax;
            res=max(res,nums[i]);
        }
        return res;
    }
};


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.

思想:有环。即:

抢了第一家就不能抢最后一家,[1,2,...,n-1]

或者抢了最后一家就不能抢第一家,[0,1,2....,n-2]

或者两者都不抢,[1,2,....,n-2],这种情况明显是属于上两种的,可以不用多余考虑。


class Solution {
public:
    int rob(vector<int>& nums) {
        if(nums.size()==0) return 0;
        else if(nums.size()==1) return nums[0];
        vector<int> a(nums.begin()+1,nums.end());
        vector<int> b(nums.begin(),nums.end()-1);
        return max(robOrigin(a),robOrigin(b));
    }
    int robOrigin(vector<int>& nums) {
        if(nums.size()==0) return 0;
        else if(nums.size()==1) return nums[0];
        else if(nums.size()==2) return max(nums[0],nums[1]);
        int premax=nums[0];
        int res=INT_MIN;
        for (int i = 2; i < nums.size(); ++i)
        {
            if(nums[i-2]>premax) premax=nums[i-2];
            nums[i]+=premax;
            res=max(res,nums[i]);
        }
        return res;
    }
};

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.


思路:两个变量ret[0]与ret[1],

ret[0]表示偷取当前节点所获得的最大收益,那么肯定不能偷取两个孩子节点,所以有ret[0]=leftret[1]+rightret[1]+root->val;

ret[1]表示不偷取当前节点所获得的最大收益,那么,孩子节点可偷取或者可不偷取,这时取最大值即可。


/**
 * 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 rob(TreeNode* root) {
        return  max(getmaxrob(root)[0],getmaxrob(root)[1]);
    }

    vector<int> getmaxrob(TreeNode*root){
        vector<int> ret(2,0);
        if(!root) return ret;
        vector<int> leftret=getmaxrob(root->left);
        vector<int> rightret=getmaxrob(root->right);
        ret[0]=leftret[1]+rightret[1]+root->val;
        ret[1]=max(leftret[0],leftret[1])+max(rightret[0],rightret[1]);
        return ret;
    }

};



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值