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. 如何同时读取 TDateTimePicker 的 Date 和 Time ?

    由于 TDateTimePicker 只能用于日期或时间,不能同时使用.如果将Kind属性设置为dtkDate,则可以指定自定义DATE格式,但忽略任何TIME格式,并且Time未定义使用该属性.如果 ...

  2. 【C语言】学不会的指针

    指针 前言: 指针是C语言程序的核心,刚开始学指针,嗯....这样呀,貌似不难呀:之后开始用指针,&p,p,*p,**p,这些指针在用的时候,额.....什么东东?每次都要想半天,特别是遇到双 ...

  3. count_if 功能模板

    count_if 功能模板 template <class InputIterator, class UnaryPredicate> typename iterator_traits< ...

  4. Intellij IDEA最全的热键表(default keymap)

    Editing Ctrl + Space Basic code completion (the name of any class, method or variable) Ctrl + Shift ...

  5. Java异常体系结构学习笔记

    异常类的继承层次       1.Throwable是所有异常类的父类,他也继承自Object.所以Throwable是一个类,而不是接口. 2.Error这个分支的异常是由于Java虚拟机内部错误导 ...

  6. Python中的 x+=x 与 x = x + x的区别

    对于Python中的可变数据类型(列表,字典)来说,+= 和 ..=..+..是不同的 加等是直接在变量的值上面进行操作,会修改了原来变量的值 先等后加会重新分配一个内存空间,不会再原有的变量值上面进 ...

  7. Python的virtualenv管理

    原文链接 虚拟环境 Python 开发中所谓的虚拟环境,就是为 Python 版本及第三方库创建独立的开发环境,使不同项目之间互不干扰.借助于虚拟环境,我们可以在同一台电脑上构建出项目 A 在基于 P ...

  8. html 随机验证码

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 如何解决div背景色半透明,里面的图片不透明问题

    用rgba可以实现,不能用opacity 背景做成透明的背景图,opacity属性影响子集的,除非把两者独立开~

  10. 个人项目 wc(java实现)

    一.Github网址: https://github.com/Clarazhangbw/Wc.exe 二.PSP表 PSP2.1 Personal Software Process Stages 预估 ...