原题链接在这里:https://leetcode.com/problems/house-robber/

题目:

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

题解:

DP问题. state 到当前house能偷到的最多钱. 转移方程 dp[i] = Math.max(dp[i-1], dp[i-2]+nums[i-1]).

优化空间. 只用维护前面两个值就够.

Time Complexity: O(n). Space: O(1).

AC Java:

 class Solution {
public int rob(int[] nums) {
if(nums == null || nums.length == 0){
return 0;
} int include = 0;
int exclude = 0;
for(int i = 0; i<nums.length; i++){
int in = include;
int ex = exclude;
exclude = Math.max(in, ex);
include = ex + nums[i];
} return Math.max(include, exclude);
}
}

跟上House Robber IIHouse Robber III.

类似Paint HouseMaximum Product SubarrayDelete and Earn.

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 II

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

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

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

  7. LeetCode——House Robber

    Description: You are a professional robber planning to rob houses along a street. Each house has a c ...

  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. Hadoop中客户端和服务器端的方法调用过程

    1.Java动态代理实例 Java 动态代理一个简单的demo:(用以对比Hadoop中的动态代理) Hello接口: public interface Hello { void sayHello(S ...

  2. Java 动态代理

    被代理的接口特点: 1. 不能有重复的接口,以避免动态代理类代码生成时的编译错误. 2. 这些接口对于类装载器必须可见,否则类装载器将无法链接它们,将会导致类定义失败. 3. 需被代理的所有非 pub ...

  3. spring aop两种配置方式(1)

    第一种:注解配置AOP注解配置AOP(使用 AspectJ 类库实现的),大致分为三步: 1. 使用注解@Aspect来定义一个切面,在切面中定义切入点(@Pointcut),通知类型(@Before ...

  4. POJ3469 & 最小割(最大流)模板

    就是一个求最小割. sol: 数据比较大,n有20000,内部相连的边有20w,这么算算就要存八九十万的边,空间显然降不下来...然而打了dinic并不觉得快很多...最快跑到3800+ms 然后跪一 ...

  5. POJ 2480 (约数+欧拉函数)

    题目链接: http://poj.org/problem?id=2480 题目大意:求Σgcd(i,n). 解题思路: 如果i与n互质,gcd(i,n)=1,且总和=欧拉函数phi(n). 如果i与n ...

  6. 移动端 设计与开发经验之ViewPort

    Viewport :字面意思为视图窗口,在移动 web 开发中使用.表示将设备浏览器宽度虚拟成一个特定的值(或计算得出),这样利于移动 web 站点跨设备显示效果基本一致. 基本写法: <met ...

  7. UVA 11021 C - Tribles(概率DP)

    记忆化就可以搞定,比赛里都没做出来,真的是态度有问题啊... #include <iostream> #include<cstdio> #include<cstring& ...

  8. FPGA的典型应用领域

    本文关键字:fpga应用,fpga应用领域, fpga培训,FPGA应用开发入门与典型实例 一.数据采集和接口逻辑领域 1.FPGA在数据采集领域的应用 由于自然界的信号大部分是模拟信号,因此一般的信 ...

  9. linux下查看内存的命令

    top能显示系统内存.我们常用的Linux下查看内容的专用工具是free命令. 下面是对内存查看free命令输出内容的解释: total:总计物理内存的大小. used:已使用多大. free:可用有 ...

  10. java工程展示问题

    当java工程这样展示时,需要选择window---->Open Perspective----->java改变java工程的展示方式