其他分享
首页 > 其他分享> > 213. 打家劫舍 II

213. 打家劫舍 II

作者:互联网

你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金。这个地方所有的房屋都 围成一圈 ,这意味着第一个房屋和最后一个房屋是紧挨着的。同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警 。

给定一个代表每个房屋存放金额的非负整数数组,计算你 在不触动警报装置的情况下 ,今晚能够偷窃到的最高金额。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/house-robber-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution {

    private int rob(int[] nums, int start, int end) {
        int get = 0, notGet = 0;
        for (int i = start; i <= end; ++i) {
            int t = get;
            get = notGet + nums[i];
            notGet = Math.max(t, notGet);
        }

        return Math.max(get, notGet);
    }

    public int rob(int[] nums) {
        return Math.max(nums[0] + rob(nums, 2, nums.length - 2), rob(nums, 1, nums.length - 1));
    }
}

标签:偷窃,213,相邻,int,II,start,房屋,小偷,打家劫舍
来源: https://www.cnblogs.com/tianyiya/p/15715507.html