[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);
    }
};

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

相关文章
|
安全
Leetcode 198. House Robber
一句话理解题意,有个偷马贼晚上要偷尽可能值钱的马,但连续两头马被偷会触发报警,问他如何在不触发报警(不偷连续的两匹马)的情况下偷到总价值最高马,返回最高总价值。
50 3
|
机器学习/深度学习 C++
【PAT甲级 - C++题解】1128 N Queens Puzzle
【PAT甲级 - C++题解】1128 N Queens Puzzle
69 1
|
安全
LeetCode 213. House Robber II
你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金。这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的。同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。 给定一个代表每个房屋存放金额的非负整数数组,计算你在不触动警报装置的情况下,能够偷窃到的最高金额。
79 0
LeetCode 213. House Robber II
LeetCode 337. House Robber III
在上次打劫完一条街道之后和一圈房屋后,小偷又发现了一个新的可行窃的地区。这个地区只有一个入口,我们称之为“根”。 除了“根”之外,每栋房子有且只有一个“父“房子与之相连。一番侦察之后,聪明的小偷意识到“这个地方的所有房屋的排列类似于一棵二叉树”。 如果两个直接相连的房子在同一天晚上被打劫,房屋将自动报警。 计算在不触动警报的情况下,小偷一晚能够盗取的最高金额。
60 0
LeetCode 337. House Robber III
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
【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,就结束了。
1572 0