House Robber

时间:2015-04-06 06:23:12   收藏:0   阅读:253

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.

一维DP即可。

 1 public class Solution {
 2     public int rob(int[] num) {
 3         if(num == null || num.length == 0) return 0;
 4         int len = num.length;
 5         int[] dp = new int[len + 1];
 6         dp[1] = num[0];
 7         for(int i = 2; i <= len; i ++){
 8             dp[i] = Math.max(dp[i - 1], dp[i - 2] + num[i - 1]);
 9         }
10         return dp[len];
11     }
12 }

 

原文:http://www.cnblogs.com/reynold-lei/p/4395341.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!