码迷,mamicode.com
首页 > 其他好文 > 详细

House Robbers. 198 & 213

时间:2018-06-03 10:45:27      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:方法   ESS   数组   问题   nbsp   out   not   tom   元素   

198. House Robbers I:

问题描述:

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

思路:

用dp来做,

动态转移方程

dp[0] = 0

dp[1] = nums[0]

dp[i] = max(dp[i] + dp[i-2], dp[i-1])

由于dp[i]只与dp[i-1]和dp[i-2]有关,我们也可以用两个指针来代替

注意边界情况:数组为空

代码:

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

 

----------------------------我是下一道题分割线----------------------------------

213: House RobberII

问题描述:

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, 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: [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
             because they are adjacent houses.

Example 2:

Input: [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.

思路:

这里的数组是个循环数组,首尾是相连的,我们可以用枚举方法来举出可能的非循环数组

不包括第一个元素和不包括第二个元素

需要注意边界情况:数组为空或数组只有一个元素。

代码:

class Solution {
public:
    int rob(vector<int>& nums) {
        if(nums.size() == 0)
            return 0;
        if(nums.size() == 1)
            return nums[0];
        return max(robber(nums, 0, nums.size()-1), robber(nums, 1, nums.size()));
    }
private:
    int robber(vector<int>& nums, int left, int right){
        
        int rob = nums[left];
        int nRob = 0;
        for(int i = left + 1; i < right; i++){
            int temp = max(rob, nRob);
            rob = nRob + nums[i];
            nRob = temp;
        }
        return max(rob, nRob);
    }
};

 

House Robbers. 198 & 213

标签:方法   ESS   数组   问题   nbsp   out   not   tom   元素   

原文地址:https://www.cnblogs.com/yaoyudadudu/p/9127834.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!