本文是在学习中的总结,欢迎转载但请注明出处: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)该题是一道简单动态规划相关的题目,如果能够正确地找到其中的递推关系,那么该题就很容易了。对于n个数的数组,如果要求得其连续不相邻元素的最大值,那么我们只需求得n-1个数的最大值,以及求得n-2个数的最大值即可,这样就形成了求解该问题的子问题的最大值问题,所以很容易考虑出递推关系,假设数组为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];
	}
}

Leetcode_198_House Robber的更多相关文章

  1. [LeetCode] House Robber III 打家劫舍之三

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  2. [LeetCode] House Robber II 打家劫舍之二

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

  3. [LeetCode] House Robber 打家劫舍

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  4. 【leetcode】House Robber

    题目简述 You are a professional robber planning to rob houses along a street. Each house has a certain a ...

  5. LeetCode House Robber III

    原题链接在这里:https://leetcode.com/problems/house-robber-iii/ 题目: The thief has found himself a new place ...

  6. Leetcode House Robber II

    本题和House Robber差不多,分成两种情况来解决.第一家是不是偷了,如果偷了,那么最后一家肯定不能偷. class Solution(object): def rob(self, nums): ...

  7. Leetcode 198 House Robber

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  8. Java for LeetCode 213 House Robber II

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

  9. 【leetcode】House Robber & House Robber II(middle)

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

随机推荐

  1. Core Python Programming一书中关于深浅拷贝的错误

    该书关于深浅拷贝的论述: 6.20. *Copying Python Objects and Shallow and Deep Copies "when shallow copies are ...

  2. 详解EBS接口开发之供应商导入(补充)--错误信息处理

    check reject details on records of AP_SUPPLIER_INT SELECT s.parent_table,s.reject_lookup_code,S.LAST ...

  3. CSDN发表文章后老是待审核的原因

    最近开始在csdn上写文章,发现老是文章被 待审核 ,于是便在网上搜集了下网友们的反馈,最后做出以下的整理. 1.CSDN检测到文章中的链接大于5,就会将文章列为"待审核",这个其 ...

  4. WINDOWS系统注册表取得管理权限研究

    有的时候开发我们需要取得系统管理员权限,可以通过修改注册表实现,研究网上的各种方法,整理得一下脚本实现取得管理员权限 脚本如下 Windows Registry Editor Version 5.00 ...

  5. 03_dbcp数据源依赖jar包,DBCP中API介绍,不同过dbcp方式使用dbcp数据库连接池,通过配置文件使用dbcp数据库连接池

     DBCP数据源 使用DBCP数据源,需要导入两个jar包 Commons-dbcp.jar:连接池的实现 Common-pool.jar:连接池实现的依赖库. 导入mysql的jar包. DBC ...

  6. Java基础----Java---集合框架---泛型、泛型方法、静态方法泛型、泛型接口、泛型限定、泛型类

    泛型:jdk1.5后的新特性,用于解决安全问题,是一个安全机制. 好处: 1.将运行时的异常出现问题classcastException.转移到了编译时期.方便程序员调试解决问题,让运行事情问题减少, ...

  7. FND Debug Log(FND_LOG_MESSAGES)

    之前每个模块记录日志的方式都不同,都会把日志写到不同的文件中,对于User来说很麻烦,需要记住很多的配置,现在越来越多的模块使用FND Logging来存储日志,比如WIP,RCV,OAF...FND ...

  8. 【Unity技巧】自定义消息框(弹出框)

    写在前面 这一篇我个人认为还是很常用的,一开始也是实习的时候学到的,所以我觉得实习真的是一个快速学习工程技巧的途径. 提醒:这篇教程比较复杂,如果你不熟悉NGUI.iTween.C#的回调函数机制,那 ...

  9. Mybatis源码之Statement处理器RoutingStatementHandler(三)

    RoutingStatementHandler类似路由器,在其构造函数中会根据Mapper文件中设置的StatementType来选择使用SimpleStatementHandler.Prepared ...

  10. Integration between SharePoint 2013 and CRM 2013 (On-Premise)

    具体步骤可见下面的链接 https://community.dynamics.com/crm/b/msdynamicscrmtips/archive/2014/01/27/integration- ...