[Dynamic Programming] 198. 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.
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的更多相关文章
- [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 ...
- [LeetCode] 53. Maximum Subarray_Easy tag: Dynamic Programming
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [LeetCode] questions conclusion_ Dynamic Programming
Questions: [LeetCode] 198. House Robber _Easy tag: Dynamic Programming [LeetCode] 221. Maximal Squar ...
- [LeetCode] 198. House Robber 打家劫舍
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- 198. House Robber
题目: You are a professional robber planning to rob houses along a street. Each house has a certain am ...
- [leet code 198]House Robber
1 题目 You are a professional robber planning to rob houses along a street. Each house has a certain a ...
- 动态规划 Dynamic Programming
March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...
- Dynamic Programming
We began our study of algorithmic techniques with greedy algorithms, which in some sense form the mo ...
- HDU 4223 Dynamic Programming?(最小连续子序列和的绝对值O(NlogN))
传送门 Description Dynamic Programming, short for DP, is the favorite of iSea. It is a method for solvi ...
随机推荐
- Docker之dockerfile制作jdk镜像
目的: Dockerfile简介 Dockerfile制作jdk镜像 Dockerfile简介 了解dockerfile之前要先了解Docker基本概念和使用可参考:https://www.cnblo ...
- ELK学习笔记之logstash将配置写在多个文件
0x00 概述 我们用Logsatsh写配置文件的时候,如果读取的文件太多,匹配的正则过多,会使配置文件动辄成百上千行代码,可能会造成阅读和修改困难.这时候,我们可以将配置文件的输入.过滤.输出分别放 ...
- Windows下使用MongoDb的经验
随着NoSql广泛应用MongoDb这个Json数据库现在也被广泛使用,接下来简单介绍一下Windows下如使安装使用MongoDb. 一.安装MongoDb 1.首先去官方网址:(https://w ...
- ASP.NET SignalR 系列(五)之群组推送
在上一章介绍了 一对一推送的方式,这章重点介绍下群组推送和多人推送 群组主要就是用到了方法:Groups.Add(Context.ConnectionId, groupName); 将不同的连接id加 ...
- 执行kubectl create-f replicaset.yaml后k8s是如何工作的
参考:提高 kubectl 使用生产力[译] 英文原文:https://learnk8s.io/blog/kubectl-productivity/ Kubernetes 架构 Kubernetes ...
- 【转载】 C#中decimal.TryParse方法和decimal.Parse方法的异同之处
在C#编程过程中,decimal.TryParse方法和decimal.Parse方法都可以将字符串string转换为decimal类型,但两者还是有区别,最重要的区别在于decimal.TryPar ...
- js继承(十)
一.原型链继承[子构造函数的原型对象是父构造函数的实例][对原型属性和方法的继承]1.每个构造函数[prototype]都有一个原型对象,原型对象中都包含一个指向构造函数的指针[constructor ...
- 英语dyamaund钻石
dyamaund 英文词汇,中文翻译为金刚石的;镶钻;用钻石装饰 中文名:镶钻;钻石装饰 外文名:dyamaund 目录 释义 dyamaund 读音:[ˈdaɪəmənd, ˈdaɪmənd] ...
- 网络监听工具 嗅探器 SpyNet
配置网卡 注册 监听配置 开始捕获
- python+java全栈工程师 转 向前端的路
python的优点 简单 简单 简单 我目前在公司用python 1. 增加odoo的各种业务,成本核算.自动跑单.自动备份数据库之类的 ----odoo是国外大佬做的一个开源erp 用的python ...