Description:

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.

求数组中不相邻两个数组合的最大值。

ps.难道这就是传说中的DP

public class Solution {
public int rob(int[] nums) { if(nums.length == 0)
return 0;
if(nums.length == 1)
return nums[0];
if(nums.length == 2)
return Math.max(nums[0], nums[1]);
int[] res = new int[nums.length];
res[0] = nums[0];
res[1] = nums[1];
res[2] = nums[0] + nums[2];
for(int i=3; i<nums.length; i++) {
res[i] = Math.max(res[i-2],res[i-3]) + nums[i];
}
return Math.max(res[nums.length-1], res[nums.length-2]);
}
}

LeetCode——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 III

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

  5. LeetCode House Robber

    原题链接在这里:https://leetcode.com/problems/house-robber/ 题目: You are a professional robber planning to ro ...

  6. Leetcode House Robber II

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

  7. [LeetCode]House Robber II (二次dp)

    213. House Robber II     Total Accepted: 24216 Total Submissions: 80632 Difficulty: Medium Note: Thi ...

  8. LeetCode House Robber 家庭劫犯(dp)

    题意:有一个整数序列,从中挑出一些数字,使得总和是最大,前提是,相邻的两个数字中只能挑其一.比如1 2 3 就只能挑2或者1和3. 思路:很直观的题,dp思想.降低规模,从小规模开始考虑.如果只有两个 ...

  9. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

随机推荐

  1. RP2833 指示灯说明

    ARM-IO9           OUT0        PA8 备用 P5-A4管脚,可以连接74HC164D级联 ARM-IO10         OUT7        PA1       3 ...

  2. CKFinder 弹出窗口操作并设置回调函数

    CKFinder 弹出窗口操作并设置回调函数 官方例子参考CKFinderJava-2.4.1/ckfinder/_samples/popup.html 写一个与EXT集成的小例子 Ext.defin ...

  3. ArrayDeque

    ArrayDeque是一个基于数组的,非线程安全的,没有容量大小限制的双端队列实现 下面这张图就是添加了一些元素的数据结构图,其中head指向数据结构中的头部元素,tail指向数据结构中最后一个元素. ...

  4. java-pageEncoding与contentType的区别

    pageEncoding是jsp文件本身的编码 contentType的charset是指服务器发送给客户端时的内容编码 JSP要经过两次的“编码”,第一阶段会用pageEncoding,第二阶段会用 ...

  5. Jquery easyui tree 一些常见操作

    Tree: easyui tree的异步加载实现很简单,easyui的中文API文档中有实例(http://api.btboys.com/easyui/)——创建异步树形菜单,就是在tree node ...

  6. 封装自己的yQuery

    function myAddEvent(obj, sEv, fn) { if (obj.attachEvent) { obj.attachEvent('on' + sEv, fn) } else { ...

  7. [从jQuery看JavaScript]-变量与作用域链

    jQuery片段: var // Will speed up references to window, and allows munging its name. window = this, //  ...

  8. e656. 创建基本图形

    Shape line = new Line2D.Float(x1, y1, x2, y2); Shape arc = new Arc2D.Float(x, y, w, h, start, extent ...

  9. ubuntu安裝 R RStudio

    sudo apt--i386.deb ref: http://blog.csdn.net/lichangzai/article/details/39376117

  10. (记录)eclipse常用设置步骤

    代码风格文件导入: https://blog.csdn.net/wangming520liwei/article/details/53911736 注释中的author修改: https://jing ...