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);
        }
    };
  • 相关阅读:
    ejs不能读取js变量??????
    技术架构
    测试框架那些事儿
    如何跨团队带项目?
    自定义网址导航站-聚享导航
    vue+node+mongodb前后端分离博客系统
    聚享导航chrome插件发布
    NuxtJS实战,一个博客系统
    Essay3.0发布,基于JavaScript的前后端同构博客系统
    小程序开发中的一些坑和技巧
  • 原文地址:https://www.cnblogs.com/wdw828/p/6870243.html
Copyright © 2011-2022 走看看