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.
思路
使用动态规划算出i个房子(nums.length=i
)时的最大和(元素不能相邻)。
由于元素不能相邻,可以有这样两种相邻“抢劫”的方式:
- 两个元素间隔一个元素
- 两个元素间隔两个元素
代码
首先用一个比较直白的方法。
假设第i个元素为nums[i], 前i-3个元素的最大和sum1,前i-2个元素的最大和sum2,前i-1个元素的最大和sum3,则前i个元素的最大和sum有以下几个option:
若选中该元素,依据不能相邻原则:
sum = sum2 + nums[i]
ORsum = sum1 + nums[i]
;- nums[i]没有被选中,则
sum = sum3
;
因此前i个元素的最大和sum = max(sum1 + nums[i], sum2 + nums[i], sum3)
;
- 上手版
这个代码不是很neat。
|
下面这个方法的思想是一样的,只不过用了很巧妙的方式极大缩短了代码。
- 进阶版
|
原题链接:https://leetcode.com/problems/house-robber/