首页 > 其他 > 详细

198. House Robber

时间:2015-12-20 11:48:47      阅读:109      评论:0      收藏:0      [点我收藏+]

198. House Robber

Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy

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.

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

/**
dp[i] = max(dp[i-2],dp[i-3]...dp[1])+nums[i];
[9,8,9,20,8]
[1,2,3,55,54,2]
*/

198. House Robber

原文:http://www.cnblogs.com/zengzy/p/5060463.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!