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. 2019-7-17 正则表达式和re模块

    一.re模块与正则表达式之间的关系 正则表达式不是python独有的,它是一门独立的技术 所有的编程语言都可以使用正则 但是如果你想在python中使用,你就必须依赖于re模块 正则的官方定义:正则表 ...

  2. ZYNQ笔记(3):GPIO的使用(MIO、EMIO)——led灯

    一.GPIO原理 1.GPIO介绍 程序员通过软件代码可以独立和动态地对每个 GPIO 进行控制,使其作为输入.输出或中断. (1)通过一个加载指令,软件可以读取一个 GPIO 组内所有 GPIO 的 ...

  3. laravel 自定义验证 Validator::extend

    laravel 自定义验证 $messages = [ 'name.integer' => '名字不能为整型', 'name.max' => '长度不能超过5', ]; public st ...

  4. emmet html缩写

    HTML缩写 Emmet使用类似于CSS选择器的语法来描述元素在生成的树中的位置和元素的属性. 元素 您可以使用元素的名字,如div或p以生成 HTML标签. Emmet没有一组预定义的可用标签名称, ...

  5. 关于justify-content属性的再学习(区分三个属性)

    justify-content属性: 用来表示可伸缩项目在主轴方向上的对齐方式: 取值范围为flex-start,flex-end,center,space-between,space-around: ...

  6. 【转载】C#中Convert.ToDecimal方法将字符串转换为decimal类型

    在C#编程过程中,可以使用Convert.ToDecimal方法将字符串或者其他可转换为数字的对象变量转换为十进制decimal类型,Convert.ToDecimal方法有多个重载方法,最常使用的一 ...

  7. python3基础之“术语表(2)”

    51.编程: 让计算机执行的指令. 52.代码: 让计算机执行的命令. 53.底层编程语言: 与高级语言相比,更接近二进制的语言. 54.高级编程语言: 读起来像英语的易于理解的语言. 55.汇编语言 ...

  8. 【DATAGUARD】物理dg的failover切换(六)

    [DATAGUARD]物理dg的failover切换(六) 一.1  BLOG文档结构图 一.2  前言部分 一.2.1  导读 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你 ...

  9. EF 拉姆达 linq if else (整理)

    首先想到: var data0 = db.T_Plants2; //这里加.AsQueryable() ) { .Where(d => d.NaturalEcosystem == true); ...

  10. django模板和静态文件

    1.为什么要使用模板 在上一篇博文中,提到了HttpReponse,但是HttpReponse只能传送字符串,如果要构建一个网页,那么工作量就会十分巨大.模板是一种方便的标签,存在于HTML文件中,我 ...