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. 使用C#创建windows 服务

    创建项目选择Windows Service 创建好项目之后,在生成的Service1.cs的设计页面右键选择Add Installer,会生成一个ProjectInstaller.cs.这个文件中有两 ...

  2. eclipse安装使用教程

    eclipse安装使用教程 很多人都知道要用eclipse来做java开发,但很多的新手朋友却不知道怎么下载和安装eclipse. 下面给你介绍一下怎么下载和安装eclipse来用于自己的学习或者项目 ...

  3. Drupal的目录结构

    ①.includes 文件夹 存放Drupal程序头文件. Drupal的一些函数和变量的定义,均可在此文件夹下的文件中找到.这 些文件都是以.inc结尾的. ②.misc文件夹 Drupal所用的其 ...

  4. BeamNG.drive物理引擎评鉴

    BeamNG.drive是一款由BeamNG公司开发并于2013年首次发布的软体物理模拟游戏.作为模拟游戏,特别是物理模拟的粉丝,我早早就开始使用BeamNG.drive.我立即对崩溃的准确性和细节印 ...

  5. Oracle重置序列

    oracle序列创建以后,如果想重置序列从 0 开始,逐渐递增1,可以采用如下存储过程: create or replace procedure reset_seq( p_seq_name in va ...

  6. 解决Spring框架的Dao层改用@Repository注解,无法使用JdbcDaoSupport的问题

    解决Spring框架的Dao层改用@Repository注解,无法使用JdbcDaoSupport的问题 Alternatively, create an own implementation of ...

  7. 用大白话揭开Ajax长轮询(long polling)的神秘面纱

    在看这篇Ajax长轮询之前可以先看看Ajax轮询技术(没有长),有助于理解: Ajax长轮询属于Ajax轮询的升级版,在客户端和服务端都进行了一些改造,使得消耗更低,速度更快. "不间断的通 ...

  8. npm常用命令汇总

    npm是一个node包管理和分发工具,已经成为了非官方的发布node模块(包)的标准. 有了npm,可以很快的找到特定服务要使用的包,进行下载.安装以及管理已经安装的包. 1.npm install ...

  9. 关于datatable导出execl

    导出主要考虑响应流的问题 curContext.Response.ContentType = "application/vnd.ms-excel"; curContext.Resp ...

  10. 丰富您设计的10个CSS3效果库

    Magic CSS3 Animations Magic CSS3 Animations是一个CSS3动画包,拥有一些特效可以你的Web项目中免费使用.拥有像金光闪闪,角度,旋转,炸弹等特殊效果.使用简 ...