zoukankan      html  css  js  c++  java
  • 198. House Robber

    Problem statement:

    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.

    Solution one: DP(AC)

    In fact, this problem requires the max sum of nonadjacent numbers in an array. The basic idea is dynamic programming.

    This is my first solution.

    dp[i] is the max sum so far.

    DP formula:

    dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]); ---> current max sum is the max val between rob current house and not rob.

    rob current house  not rob

    Initialization:

    since dp[i] depends on dp[i - 2], we need the value of dp[i - 1] and dp[i - 2]. 

    For dp[0] = nums[0].

    dp[1] = max(nums[0], nums[1]) ---> initially, I set dp[1] = nums[1], that is why I was declined since a wrong test case of [2,1,1,2].

    return dp[n].

    Time complexity is O(n). Space complexity is O(n).

    class Solution {
    public:
        int rob(vector<int>& nums) {
            if(nums.empty()){
                return 0;
            }
            if(nums.size() == 1){
                return nums[0];
            }
            if(nums.size() == 2){
                return max(nums[0], nums[1]);
            }
            int size = nums.size();
            vector<int> dp(size, 0);
            dp[0] = nums[0];
            dp[1] = max(nums[0], nums[1]);
            for(int i = 2; i < nums.size(); i++){
                dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]);
            }
            return dp.back();
        }
    };

    Solution two: DP(AC)

    It is also DP philosophy. But we use two dp array.

    r[i]: max value if rob current house

    n[i]: max value if do not rob current house

    DP formula:

    r[i] = n[i - 1] + nums[i];

    n[i] = max(r[i - 1], n[i - 1]);

    Initialization:

    r[i] and n[i] only depends on r[i - 1] and n[i - 1], we only initialize r[0] and n[0].

    r[0] = nums[0];

    n[0] = 0;

    Return max(r[n], n[n]);

    Time complexity is O(n). Space complexity is O(n).

    class Solution {
    public:
        int rob(vector<int>& nums) {
            if(nums.empty()){
                return 0;
            }
            int size = nums.size();
            vector<int> r(size, 0);
            vector<int> n(size, 0);
            r[0] = nums[0];
            for(int i = 1; i < size; i++){
                r[i] = nums[i] + n[i - 1];
                n[i] = max(r[i - 1], n[i - 1]);
            }
            return max(r.back(), n.back());
        }
    };

    Solution three:

    This is not a new idea, it optimizes the solution two.

    We can reduce space complexity from O(n) to O(1) since r[i] and n[i] only depend on r[i - 1] and n[i - 1].

    class Solution {
    public:
        int rob(vector<int>& nums) {
            if(nums.empty()){
                return 0;
            }
            int size = nums.size();
            int rob = nums[0], not_rob = 0;
            for(int i = 1; i < size; i++){
                int r = rob;
                int n = not_rob;
                rob = nums[i] + n;
                not_rob = max(r, n);
            }
            return max(rob, not_rob);
        }
    };
  • 相关阅读:
    什么是HTTP
    通过递归法解决阶梯问题(n个台阶,上楼可以一步上1阶,也可以一步上2阶,一共有多少种上楼的方法)
    在Intelli Idea中使用plantuml(plantuml时序图的使用)
    Java中if(boolean)与if(boolean=true)的区别
    实现一个Servlet程序
    退出mysql的编辑模式
    mysql数据库基本操作命令行
    通过mysql命令查看mysql服务实例支持的搜索引擎
    Mac环境下使用终端启动Mysql,并进行mysql数据库的连接
    路飞学城Python-Day4
  • 原文地址:https://www.cnblogs.com/wdw828/p/6870243.html
Copyright © 2011-2022 走看看