博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode][JavaScript]House Robber
阅读量:4569 次
发布时间:2019-06-08

本文共 1043 字,大约阅读时间需要 3 分钟。

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.

 

 


 

 

动态规划。

分两种情况

1.偷:dp[i - 2] + nums[i - 1]

2.不偷:dp[i - 1]

dp[i] = Math.max(dp[i - 2] + nums[i - 1], dp[i - 1]);

1 /** 2  * @param {number[]} nums 3  * @return {number} 4  */ 5 var rob = function(nums) { 6     if(nums.length === 0){ 7         return 0; 8     } 9     var dp = [];10     dp[0] = 0;11     dp[1] = nums[0];12     for(var i = 2; i <= nums.length; i++){13         dp[i] = Math.max(dp[i - 2] + nums[i - 1], dp[i - 1]);14     }15     return dp[nums.length] ;16 };

 

 

 

转载于:https://www.cnblogs.com/Liok3187/p/4842444.html

你可能感兴趣的文章
操作系统
查看>>
记录 一次深夜救火:datanode.data.dir
查看>>
Apache 使用 .htaccess 文件配置全站 301 跳转代码
查看>>
微信小程序 获取OpenId
查看>>
IDEA快捷操作
查看>>
android 的touch event分析
查看>>
转:C#进阶系列——WebApi 跨域问题解决方案:CORS
查看>>
实参和形参
查看>>
利用GPGPU计算大规模群落仿真行为
查看>>
BZOJ 3211: 花神游历各国【线段树区间开方问题】
查看>>
C语言sprintf和sscanf函数用法
查看>>
javascript 基础
查看>>
WAV文件格式
查看>>
WPF stringformat设置
查看>>
阻止vue事件冒泡的方法
查看>>
第十七周进度总结
查看>>
javascript面向对象基础
查看>>
利用iscroll实现上拉加载下拉刷新
查看>>
C# 中的委托和事件
查看>>
用户控件 RadioButtonList
查看>>