Leetcode213-House Robber II

25 篇文章 0 订阅
22 篇文章 0 订阅

我的新博客地址HuHaoyu’s Blog,欢迎参观。

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.

分析

类似于之前的House Robber,唯一的区别在于房屋排列是环形的,而非线性,这样头和尾的房屋是不能同时被偷窃的。因此在原有的基础上增加一个状态,即第一间房屋是否被偷窃。

采用两位二进制数表示偷窃到第i家时的状态,高位表示是否在第1家偷窃,低位表示是否在第i家偷窃。比如:

0001
不在第1家偷窃,也不在第i家偷窃不在第1家偷窃,但在第i家偷窃
1011
在第1家偷窃,但不在第i家偷窃在第1家偷窃,也在第i家偷窃

由此,到达第i家时的状态如下:

robMoney[i][0] = max(robMoney[i-1][0], robMoney[i-1][1]);
robMoney[i][1] = robMoney[i-1][0] + nums[i];
robMoney[i][2] = max(robMoney[i-1][2], robMoney[i-1][3]);
robMoney[i][3] = robMoney[i-1][2] + nums[i];

注意

  • 应特别注意的是,最后一家只能有状态00, 01, 10
  • 同时,应特殊处理只有1家的情况,此时11才是正确的状态。
  • 第1家时,01, 10是矛盾的状态,robMoney设为MIN_INT

AC代码

class Solution {
public:
    const static int MIN_INT = -999999999;

    int rob(vector<int>& nums) {
        int houseNumber = nums.size();
        if (!houseNumber) return 0;
        if (houseNumber == 1) return nums[0];
        int robMoney[houseNumber][4];

        for (int i = 0; i != houseNumber; ++i) {
            for (int j = 0; j != 4; ++j) {
                robMoney[i][j] = 0;
            }
        }

        robMoney[0][3] = nums[0];
        robMoney[0][1] = robMoney[0][2] = MIN_INT;

        for (int i = 1; i < houseNumber; ++i) {
            robMoney[i][0] = max(robMoney[i-1][0], robMoney[i-1][1]);
            robMoney[i][1] = robMoney[i-1][0] + nums[i];
            robMoney[i][2] = max(robMoney[i-1][2], robMoney[i-1][3]);
            robMoney[i][3] = robMoney[i-1][2] + nums[i];
        }

        return max(robMoney[houseNumber-1][0], robMoney[houseNumber-1][1], robMoney[houseNumber-1][2]);
    }

    int max(int x, int y) { return x > y ? x : y; }

    int max(int x, int y, int z) {
        if (x >= y && x >= z) return x;
        if (y >= x && y >= z) return y;
        return z;
    }
};

如代码或分析有误,请批评指正。谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值