zoukankan      html  css  js  c++  java
  • 【刷题-LeetCode】198 House Robber

    1. 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 1:

    Input: nums = [1,2,3,1]
    Output: 4
    Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
                 Total amount you can rob = 1 + 3 = 4.
    

    Example 2:

    Input: nums = [2,7,9,3,1]
    Output: 12
    Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
                 Total amount you can rob = 2 + 9 + 1 = 12.
    

    解1 dfs搜索。超时了。。。

    class Solution {
    public:
        int rob(vector<int>& nums) {
            vector<bool>flag(nums.size(), false);
            int money = 0, max_money = 0;
            dfs(nums, 0, flag, money, max_money);
            return max_money;
        }
        void dfs(vector<int>& nums, int i,
                 vector<bool>& flag, int &money, int &max_money){
            if(i >= nums.size()){
                if(money > max_money)max_money = money;
                return;
            }
            if(i > 0){
                if(flag[i-1] == false){
                    flag[i] = true;
                    money += nums[i];
                    dfs(nums, i+1, flag, money, max_money);
                    flag[i] = false;
                    money -= nums[i];
                }
                dfs(nums, i+1, flag, money, max_money);
            }else{
                flag[i] = true;
                money += nums[i];
                dfs(nums, i+1, flag, money, max_money);
                flag[i] = false;
                money -= nums[i];
                dfs(nums, i+1, flag, money, max_money);
            }
        }
    };
    

    解2 动态规划。dp[k]表示前k家能够抢到的最大金额,对于第k+1家:

    • 抢:第k家就不能抢,因此dp[k+1] = dp[k-1] + A[k+1]
    • 不抢:dp[k+1] = dp[k]
    class Solution {
    public:
        int rob(vector<int>& nums) {
            if(nums.size() == 0)return 0;
            if(nums.size() == 1)return nums[0];
            int dp0 = nums[0], dp1 = max(nums[0], nums[1]);
            for(int i = 2; i < nums.size(); ++i){
                int tmp = max(dp1, dp0+nums[i]);
                dp0 = dp1;
                dp1 = tmp;
            }
            return dp1;
        }
    };
    
  • 相关阅读:
    多播(组播)与广播的技术实现
    (转)单播、多播(组播)和广播的区别
    (转)简述负载均衡&CDN技术
    当领导得学会骂下属的艺术
    CSDN首页> 云计算 孙玄:解析58同城典型技术架构及演变
    VMware网络连接 桥接、NAt、host-only模式 .
    dajian
    [Android开发常见问题-4] RunTime.exec()如何以root权限执行多条指令?
    Android获取Root权限之后的静默安装实现代码示例分析
    Android
  • 原文地址:https://www.cnblogs.com/vinnson/p/13300021.html
Copyright © 2011-2022 走看看