[LintCode] House Robber II 打家劫舍之二

简介:

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.
Notice
This is an extension of House Robber.
Example
nums = [3,6,4], return 6

LeetCode上的原题,请参见我之前的博客House Robber II

解法一:

class Solution {
public:
    /**
     * @param nums: An array of non-negative integers.
     * return: The maximum amount of money you can rob tonight
     */
    int houseRobber2(vector<int>& nums) {
        if (nums.size() <= 1) return nums.empty() ? 0 : nums[0];
        vector<int> a = nums, b = nums;
        a.erase(a.begin()); b.pop_back();
        return max(rob(a), rob(b));
    }
    int rob(vector<int> &nums) {
        if (nums.size() <= 1) return nums.empty() ? 0 : nums[0];
        vector<int> dp{nums[0], max(nums[0], nums[1])};
        for (int i = 2; i < nums.size(); ++i) {
            dp.push_back(max(dp[i - 2] + nums[i], dp[i - 1]));
        }
        return dp.back();
    }
};

解法二:

class Solution {
public:
    /**
     * @param nums: An array of non-negative integers.
     * return: The maximum amount of money you can rob tonight
     */
    int houseRobber2(vector<int>& nums) {
        if (nums.size() <= 1) return nums.empty() ? 0 : nums[0];
        return max(rob(nums, 0, nums.size() - 1), rob(nums, 1, nums.size()));
    }
    int rob(vector<int> &nums, int left, int right) {
        int a = 0, b = 0;
        for (int i = left; i < right; ++i) {
            int m = a, n = b;
            a = n + nums[i];
            b = max(m, n);
        }
        return max(a, b);
    }
};

本文转自博客园Grandyang的博客,原文链接:打家劫舍之二[LintCode] House Robber II ,如需转载请自行联系原博主。

相关文章
|
安全
Leetcode 198. House Robber
一句话理解题意,有个偷马贼晚上要偷尽可能值钱的马,但连续两头马被偷会触发报警,问他如何在不触发报警(不偷连续的两匹马)的情况下偷到总价值最高马,返回最高总价值。
50 3
LeetCode 337. House Robber III
在上次打劫完一条街道之后和一圈房屋后,小偷又发现了一个新的可行窃的地区。这个地区只有一个入口,我们称之为“根”。 除了“根”之外,每栋房子有且只有一个“父“房子与之相连。一番侦察之后,聪明的小偷意识到“这个地方的所有房屋的排列类似于一棵二叉树”。 如果两个直接相连的房子在同一天晚上被打劫,房屋将自动报警。 计算在不触动警报的情况下,小偷一晚能够盗取的最高金额。
61 0
LeetCode 337. House Robber III
|
安全
LeetCode 213. House Robber II
你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金。这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的。同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。 给定一个代表每个房屋存放金额的非负整数数组,计算你在不触动警报装置的情况下,能够偷窃到的最高金额。
80 0
LeetCode 213. House Robber II
Leetcode:House Robber II
Leetcode:House Robber II
127 0
Leetcode:House Robber II
CodeForces 1195C Basketball Exercise (线性DP)
CodeForces 1195C Basketball Exercise (线性DP)
119 0
HDU-3635,Dragon Balls(有点难度的并查集。。。)
HDU-3635,Dragon Balls(有点难度的并查集。。。)
【LeetCode】House Robber III(337)
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 automaticall
98 0
|
Java
java之 ------------[LeetCode] House Robber 打家劫舍
刚开始做这一道题感觉卧槽,这不简单吗,直接去把数组下标和2取余的数相加再把剩下的数相加,比较这两个和谁大就输出谁,不就行了,但是啊,我操,事实证明,我还是太天真了,我操出现[2,1,1,2]这种情况,我当时还怀疑为什么那么简单后来一想,我操,这不是动态规划吗,于是乎,恶补一下怎么实现动态规划的,说白了,动态规划就是把大的数据拆成小的数据,如我想计算f(10),我就要计算出f(9)+1,然后我想计算出f(9)=f(8)+1,递推的方式直到f(1)=f(0)+1,就结束了。
1574 0