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);
        }
    };
  • 相关阅读:
    ios更改UITabBarController背景以及选中背景图片的方法
    ios7 sdk 新特性
    AES加密算法原理
    iOS 获取手机的型号,系统版本,软件名称,软件版本
    iOS8中的UIActionSheet添加UIDatePicker后,UIDatePicker不显示问题
    iOS 8 定位失败问题
    利用CMake和OpenCV源代码生成Visual Studio工程
    dm8127前段采集和抓拍
    c++课程设计
    github安卓游戏
  • 原文地址:https://www.cnblogs.com/wdw828/p/6870243.html
Copyright © 2011-2022 走看看