您的位置:首页 > 其它

Leetcode House Robber

2016-06-15 11:59 316 查看
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.

Difficulty: Easy

Solution: DP

public class Solution {
public int rob(int[] nums) {
int len = nums.length;
if(len == 0) return 0;
int ans1 = nums[0];
int ans2 = 0;
for(int i = 1; i < len; i++){
int temp1 = ans1;
int temp2 = ans2;
ans1 = Math.max(temp1,temp2 + nums[i]);
ans2 = Math.max(temp1, temp2);
}
return Math.max(ans1, ans2);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: