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

House Robber

时间:2015-04-26 22:36:06      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

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.

思路:

  01 背包问题的变形而已

我的代码:

技术分享
public class Solution {
    public int rob(int[] nums) {
        if(nums == null || nums.length == 0)    return 0;
        int len = nums.length;
        if(len == 1) return nums[0];
        if(len == 2) return Math.max(nums[0],nums[1]);
        int[] result = new int[len];
        result[0] = nums[0];
        result[1] = Math.max(nums[0],nums[1]);
        for(int j = 2; j < len; j++)
        {
            result[j] = Math.max(result[j-1],nums[j]+result[j-2]);
        }
        return result[len-1];
        
    }
}
View Code

 

House Robber

标签:

原文地址:http://www.cnblogs.com/sunshisonghit/p/4458511.html

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