[LintCode] House Robber 打家劫舍

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.

Example

Given [3, 8, 4], return 8.
Challenge

O(n) time and O(1) memory.

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

解法一:

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

解法二:

class Solution {
public:
    /**
     * @param A: An array of non-negative integers.
     * return: The maximum amount of money you can rob tonight
     */
    long long houseRobber(vector<int> A) {
        long long a = 0, b = 0;
        for (int i = 0; i < A.size(); ++i) {
            if (i % 2 == 0) {
                a += A[i];
                a = max(a, b);
            } else {
                b += A[i];
                b = max(a, b);
            }
        }
        return max(a, b);
    }
};

解法三:

class Solution {
public:
    /**
     * @param A: An array of non-negative integers.
     * return: The maximum amount of money you can rob tonight
     */
    long long houseRobber(vector<int> A) {
        long long a = 0, b = 0;
        for (int i = 0; i < A.size(); ++i) {
            long long m = a, n = b;
            a = n + A[i];
            b = max(m, n);
        }
        return max(a, b);
    }
};
时间: 2024-11-09 02:46:34

[LintCode] House Robber 打家劫舍的相关文章

[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 o

[LeetCode] House Robber 打家劫舍

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 autom

[LeetCode] 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.

代码题(42)— 打家劫舍

1.198. 打家劫舍 你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警. 给定一个代表每个房屋存放金额的非负整数数组,计算你在不触动警报装置的情况下,能够偷窃到的最高金额. 示例 1: 输入: [1,2,3,1] 输出: 4 解释: 偷窃 1 号房屋 (金额 = 1) ,然后偷窃 3 号房屋 (金额 = 3).   偷窃到的最高金额 = 1 + 3 = 4 .

[LeetCode] 656. Coin Path 硬币路径

Given an array A (index starts at 1) consisting of N integers: A1, A2, ..., AN and an integer B. The integer Bdenotes that from any place (suppose the index is i) in the array A, you can jump to any one of the place in the array A indexed i+1, i+2, …

213 House Robber II 打家劫舍 II

注意事项: 这是 打家劫舍 的延伸.在上次盗窃完一条街道之后,窃贼又转到了一个新的地方,这样他就不会引起太多注意.这一次,这个地方的所有房屋都围成一圈.这意味着第一个房子是最后一个是紧挨着的.同时,这些房屋的安全系统与上次那条街道的安全系统保持一致.给出一份代表每个房屋存放钱数的非负整数列表,确定你可以在不触动警报的情况下盗取的最高金额. 详见:https://leetcode.com/problems/house-robber-ii/description/ class Solution {

198. 打家劫舍 | House Robber

ou 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 automa

[Swift]LeetCode213. 打家劫舍 II | House Robber II

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent

LintCode 392 House Robber

// Ref: https://segmentfault.com/a/1190000003811581// Ref: http://www.cnblogs.com/grandyang/p/4383632.html /*如果选择了抢劫上一个屋子,那么就不能抢劫当前的屋子,所以最大收益就是抢劫上一个屋子的收益如果选择抢劫当前屋子,就不能抢劫上一个屋子,所以最大收益是到上一个屋子的上一个屋子为止的最大收益,加上当前屋子里有的钱*/ 1 public static long houseRobber(in