Leetcode_198_House Robber

本文是在学习中的总结。欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/47680663



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.



思路:

(1)这道题非常有意思。在这里就不翻译成中文了。将题目内容转化为通俗易懂的形式为:给定一个整数数组Arr,求解数组中连续的不相邻元素的和的最大值。比如:对于数组中的元素A1,A2,A3,A4。则须要推断A1+A3,A1+A4,A2+A4中的最大值即为所求。

(2)该题是一道简单动态规划相关的题目。假设可以正确地找到当中的递推关系。那么该题就非常easy了。对于n个数的数组,假设要求得其连续不相邻元素的最大值,那么我们仅仅需求得n-1个数的最大值,以及求得n-2个数的最大值就可以,这样就形成了求解该问题的子问题的最大值问题,所以非常easy考虑出递推关系,假设数组为Arr[],n个数的数组相应的不相邻连续元素的最大值用函数f(n)表示,则有f(n) = max{f(n-1), f(n-2)+A[n-1]},当中n>=2,f(n)也称为递推关系。

当中f(n-1)为n-1个元素的最大值。f(n-2)+Arr[n-1]为n-2个元素的最大值加上数组第n个元素的值,由于要求元素不能相邻。所以会跳过第n-1个元素。这个应该非常好理解。

对动态规划感兴趣的同学可以看看网上有关动态规划的文章。个人认为非常有必要学习动态规划的思想。

(3)详情见下方代码。希望本文对你有所帮助。


算法代码实现例如以下:

package leetcode;

/**
 * 
 * @author liqq
 *
 */
public class House_Robber {

	public static int rob(int[] nums) {

		if (nums == null || nums.length == 0)
			return 0;

		int len = nums.length;
		int[] rt = new int[len];

		if (len == 1)
			return nums[0];

		if (len == 2) {
			return nums[0] > nums[1] ? nums[0] : nums[1];
		}

		for (int i = 0; i < len; i++) {
			if (i == 0) {
				rt[i] = nums[i];
			} else if (i == 1) {
				rt[i] = Math.max(rt[i - 1], nums[i]);
			} else {
				rt[i] = Math.max(rt[i - 1], rt[i - 2] + nums[i]);
			}
		}
		return rt[len - 1] > rt[len - 2] ? rt[len - 1] : rt[len - 2];
	}
}

原文地址:https://www.cnblogs.com/mfmdaoyou/p/7096221.html