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.

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.
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.

1 Form bottom-up approach

/**
* @param {number[]} nums
* @return {number}
*/ var rob = function(nums) {
// if there is nothing, return 0
if (nums.length === ) {
return ;
} // if there is only one value, return itself
if (nums.length === ) {
return nums[];
} // if there are two values, return the larger one
if (nums.length === ) {
return Math.max(nums[], nums[]);
} // if there are more than two values
// copy the nums and preappend leading zero
// to avoid extra if else check for sums[i - 3],
// which can be out of index error
let sums = [].concat(nums); for (let i = ; i < sums.length; i++) {
sums[i] = Math.max(sums[i - ] + sums[i], sums[i - ] + sums[i]);
} return Math.max(
sums[sums.length - ],
sums[sums.length - ]
)
};

2. Recursive:

var rob = function(nums) {
const helper = (nums, i, sums) => {
// if there is nothing, return 0
if (nums.length === ) {
return ;
} if (nums.length === ) {
return nums[];
} if (nums.length === ) {
return Math.max(nums[], nums[]);
} // if there is only one value, return itself
if (i === ) {
sums[] = nums[];
return helper(nums, i+, sums);
} // if there are two values, return the larger one
if (i === ) {
sums[] = Math.max(nums[], nums[]);
return helper(nums, i+, sums);
} if (i >= nums.length) {
return Math.max(sums[sums.length - ], sums[sums.length - ]);
} const step1 = sums[i-] + nums[i];
const step2 = ( sums[i-] || ) + nums[i]; const larger = Math.max(step1, step2);
sums[i] = larger; return helper(nums, i+, sums);
}; return helper(nums, , []);
}

[Dynamic Programming] 198. House Robber的更多相关文章

  1. [LeetCode] 198. House Robber _Easy tag: Dynamic Programming

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

  2. [LeetCode] 53. Maximum Subarray_Easy tag: Dynamic Programming

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  3. [LeetCode] questions conclusion_ Dynamic Programming

    Questions: [LeetCode] 198. House Robber _Easy tag: Dynamic Programming [LeetCode] 221. Maximal Squar ...

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

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

  5. 198. House Robber

    题目: You are a professional robber planning to rob houses along a street. Each house has a certain am ...

  6. [leet code 198]House Robber

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

  7. 动态规划 Dynamic Programming

    March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...

  8. Dynamic Programming

    We began our study of algorithmic techniques with greedy algorithms, which in some sense form the mo ...

  9. HDU 4223 Dynamic Programming?(最小连续子序列和的绝对值O(NlogN))

    传送门 Description Dynamic Programming, short for DP, is the favorite of iSea. It is a method for solvi ...

随机推荐

  1. php数组到json的转变

    今天做项目遇到个问题,一个接口,输出二维数组,前端说他要的数据格式是数组,而不是对象,就像上个数据一样,我当时就懵逼了,,,什么对象?我明明输出的是数组啊...然后我看了看我返回的json串,emmm ...

  2. day26——tyoe元类与object的联系、反射、函数与方法的区别、双下方法

    day26 type元类与object联系 type 获取对象从属于的类 python 中一切皆对象, 类在某种意义上也是一个对象,python中自己定义的类,以及大部分内置类,都是由type元类(构 ...

  3. sizeof运算符和strlen()函数

    首先放上代码和运行结果.(在VC6.0上运行) #include<stdio.h> #include<string.h> int main(void) { char s1[]= ...

  4. python知识架构

    如果看不清可以右键图片打开新的标签页 原文链接:https://blog.csdn.net/oscer2016/article/details/80129284

  5. golang爬虫

  6. 【题解】Luogu P5338 [TJOI2019]甲苯先生的滚榜

    原题传送门 这题明显可以平衡树直接大力整,所以我要说一下线段树+树状数组的做法 实际线段树+树状数组的做法也很暴力 我们先用树状数组维护每个ac数量有多少个队伍.这样就能快速求出有多少队伍ac数比现在 ...

  7. 【写法】为什么if判断中,值要倒着写

    =============================================== 2019/8/27_第1次修改                       ccb_warlock == ...

  8. Drools7 Hello Wrold 入门详细步骤--系列01课

    一.什么叫规则引擎?规则--->写在文档上引擎--->在java代码上,引用这个文档上的规则 二.drools规则引擎有什么用?简单来说就是将多变的规则,从业务代码中剥离出来(当规则变了之 ...

  9. oracle学习笔记(三)

    索引: drop table test1 purge; drop table test2 purge; drop table test3 purge; drop table t purge; crea ...

  10. 阿里巴巴 Java 开发手册(四): OOP 规约

    . [强制]避免通过一个类的对象引用访问此类的静态变量或静态方法,无谓增加编译器解析成 本,直接用类名来访问即可. 2. [强制]所有的覆写方法,必须加@Override 注解. 说明:getObje ...