URL : 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 police.

Example 1:

Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
  Total amount you can rob = 1 + 3 = 4.

Example 2:

Input: [2,7,9,3,1]
Output: 12
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
  Total amount you can rob = 2 + 9 + 1 = 12.

解决方案

/**
* 罗列出每天偷Y和不偷N的盈利情况
* Y(n) = nums[n] + N(n-1); //前一天肯定不能偷,再加上今天偷的金额
* N(n) = Max(Y(n-1), N(n-1)); //前一天偷或者不偷均可,选取最大值。因为今天不再偷,所以不必加另外的金额
* 最后的结果就是Max(Y(n),N(n))...
*/ import java.lang.Math;
class Solution {
public int rob(int[] nums) { if(nums == null || nums.length < 1){
return 0;
} int pre_yes = 0;
int pre_no = 0; int cur_yes = 0;
int cur_no = 0; for(int i = 0; i < nums.length; ++i){
cur_yes = nums[i] + pre_no;
cur_no = Math.max(pre_yes,pre_no); pre_yes = cur_yes;
pre_no = cur_no;
}
return Math.max(pre_yes,pre_no); }
}

动态规划 - 198. House Robber的更多相关文章

  1. 198. House Robber(动态规划)

    198. House Robber You are a professional robber planning to rob houses along a street. Each house ha ...

  2. 198. House Robber,213. House Robber II

    198. House Robber Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy You are a profess ...

  3. leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)

    House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...

  4. Leetcode 198 House Robber 动态规划

    题意是强盗能隔个马抢马,看如何获得的价值最高 动态规划题需要考虑状态,阶段,还有状态转移,这个可以参考<动态规划经典教程>,网上有的下的,里面有大量的经典题目讲解 dp[i]表示到第i匹马 ...

  5. 198. House Robber(动态规划)

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  6. [LeetCode] 198. House Robber 打家劫舍

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  7. (easy)LeetCode 198.House Robber

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  8. 【LeetCode】198 - House Robber

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  9. 一道简单的动态规划题目——House Robber

    一.题目 House Robber(一道Leetcode上的关于动态规划的简单题目)具体描述如下: There is a professional robber planning to rob hou ...

随机推荐

  1. ArrayList 实现随机点名

    package lijun.cn.demo1; import java.util.ArrayList; import java.util.Random; public class CallName { ...

  2. bzoj1027 状压dp

    https://www.lydsy.com/JudgeOnline/problem.php?id=1072 题意 给一个数字串s和正整数d, 统计s有多少种不同的排列能被d整除 试了一下发现暴力可过 ...

  3. 兄弟连Linux运维学习笔记

    最新经典linux运维兄弟连Linux运维学习笔记... --------------- 全程1.5倍播放.加油我一定可以学完Linux----------------------Unix与Linux ...

  4. VirtualBox使用入门

    VirtualBox使用入门 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 能玩虚拟机的人多少是懂点运维的,因此我就不跟大家介绍啥事虚拟化了.关于虚拟化产品大家用的应该也都大同小异 ...

  5. Nginx安装及配置详解包括windows环境

    nginx概述 nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器:nginx可以作为一个HTTP服务器进行网站的发布处理,另外 ...

  6. myeclipse使用步骤总结

    1.安装和破解:http://www.cnblogs.com/haimishasha/p/5203069.html 2.修改编码方式:http://www.cnblogs.com/haimishash ...

  7. struct sock注释

    针对 struct sock 的一些注释: struct sock { /* Socket demultiplex comparisons on incoming packets. */ __u32  ...

  8. SpringMvc+jQuery 文件拖拽上传、选择上传

    最近做了个简易的基于boostrap的文件上传功能,jsp版本的,后续会完善更多的功能,不过现在已经能用了,需要的小伙伴,直接引用下面的文件内容直接copy到自己的项目中就ok了,效果如图: file ...

  9. canvas绘图history妙用

    function palette(canvas,ctx){ //初始化画布内部元素默认样式 this.strokeColor = 'red'; //默认选中红色触发颜色 this.fillColor ...

  10. java NIO入门【原】

    server package com.server; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import jav ...