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. MyEclipse SVN 下面切换用户的解决方案

    configuration\org.eclipse.core.runtime\.keyring 删除MyEclipse下面的文件. 或者修改服务器端的用户密码.

  2. Android——列表视图 ListView(一)Arrayadapter

    一.ArrayAdapter 只显示文字 activitylistview_layout.xml <?xml version="1.0" encoding="utf ...

  3. JavaScrip——DOM操作(查找HTML元素/修改元素)

    innerHTML 1.查找元素——document.getElementById("intro") 2.输出查找的结果: (1)var a=document.getElement ...

  4. 【高可用HA】Apache (4) —— Mac下配置Apache Httpd负载均衡(Load Balancer)之mod_jk

    Mac下配置Apache Httpd负载均衡(Load Balancer)之mod_jk httpd版本: httpd-2.4.17 jk版本: tomcat-connectors-1.2.41 参考 ...

  5. anroid 广播

    广播接收者(BroadcastReceiver)用于接收广播Intent,广播Intent的发送是通过调用Context.sendBroadcast().Context.sendOrderedBroa ...

  6. QFTPERROR lists

  7. BCM_I2C函数更改

    版本:sdk-xgs-robo- 平台:BCM53344 应用:控制POE芯片 描述:POE控制芯片使用PD69200,使用i2c与其通信,每次需要发送15字节数据,并接受15字节的返回数据. 1.更 ...

  8. C# 发邮件 服务器响应为: 5.7.0 Must issue a STARTTLS command first

    The SMTP server requires a secure connection or the client was not authenticated. The server respons ...

  9. (转)一种开源的跨平台视频开发框架:VideoLAN - VLC media player

    VLC原先是几个法国的大学生做的项目,后来他们把VLC作为了一个开源的项目,吸引了来自世界各国的很多优秀程序员来共同编写和维护VLC,才逐渐变成了现在这个样子.至于为什么叫VideoLan Clien ...

  10. Floyd算法实例

    ~ 当k=0时,我们关注的是邻接矩阵的第0行和第0列,即顶点0的入边和出边: 考察矩阵中其他元素,如果元素D[i][j]向第0行和第0列的投影D[0][j]和D[i][0]都有值,就说明原图中从 i ...