本文是在学习中的总结,欢迎转载但请注明出处: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. OpenCV:Mat元素访问方法、性能、代码复杂度以及安全性分析

    欢迎转载,尊重原创,所以转载请注明出处: http://blog.csdn.net/bendanban/article/details/30527785 本文讲述了OpenCV中几种访问矩阵元素的方法 ...

  2. Android启动Activity

    Android和java启动的区别 不同于使用 main() 方法启动应用的其他编程范例,Android 系统会通过调用对应于其生命周期中特定阶段的特定回调方法在 Activity 实例中启动代码.有 ...

  3. 《高性能MySQL》读书笔记(上)

    <High Performance MySQL>真是本经典好书,从应用层到数据库到硬件平台,各种调优技巧.常见问题全都有所提及.数据库的各种概念技巧平时都有接触,像索引.分区.Shardi ...

  4. 无网络环境下安装Dynamics CRM

    在安装CRM时会需要很多的组件支持,没有这些组件是没法安装的,一般我们都是选择机器联网后在线安装,但也有特殊情况确实不能联网的,可参考这篇文章 https://blogs.msdn.microsoft ...

  5. Ubuntu下配置Telnet服务器

    1. 首先介绍linux中的守护进程 在Linux系统中有一个特殊的守护进程inetd(InterNET services Daemon),它用于Internet标准服务,通常在系统启动时启动.通过命 ...

  6. JDBC的使用五大步骤以及查询操作-数据库编程(二)

    jdbc的使用步骤 1.加载jdbc的驱动. 2.打开数据库的连接. 3.建立一个会话,然后执行增删改查等基本的操作. 4.对结果进行处理 5.对环境进行清理,比如关闭会话等. 查询操作 首先用Cla ...

  7. FFmpeg源代码简单分析:avformat_find_stream_info()

    ===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...

  8. FFmpeg源代码简单分析:avformat_open_input()

    ===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...

  9. Android初级教程:对文件和字符串进行MD5加密工具类

    转载请注明出处:http://blog.csdn.net/qq_32059827/article/details/52200008   点击打开链接 之前写过一篇博文,是针对字符串进行md5加密的.今 ...

  10. Java基础---Java---IO流-----BufferedReader、BufferedWriter、缓冲区、装饰设计模式及和继承的区别

    IO流 IO流用来处理设备之间的数据传输 java对数据的操作是过流的方式 流按操作数据分为两种:字节流与字符流 流按流向分为:输入流,输出流. IO流常用基类 字节流的抽象基类:InputStrea ...