leetcode:House Robber(动态规划dp1)

时间:2023-01-07 22:48:29

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、当数组为空(即没有可抢劫的房子)时,返回0

2、当数组元素个数为1时,这时最大抢劫金额(记为maxrob[1]) 即为nums[0].

3、当数组元素个数大于1时,我们运用动态规划(dp)的思想从下向上进行递推:maxrob[2]取值为前两个元素中的大者,maxrob[3]取值为元素1,3的组合同元素2相比的大者。我们容易得到递归方程:

maxrob[i]=a+nums[i-1];

a=(maxrob[i-2]>maxrob[i-3])?maxrob[i-2]:maxrob[i-3]

即组合当前元素nums[i-1]时,(考虑到不相邻的特征)需比较组合了其前面2位的元素nums[i-3]的最大组合maxrob[i-2]和组合了其前面3位的元素nums[i-4]的最大组合maxrob[i-3]

4、最后还需比较下组合了最后元素的情况和组合了倒数第二位元素的情况,选取较大者作为返回值即可。

代码如下:

class Solution {
public:
int rob(vector<int>& nums) {
int n=nums.size();
if(nums.empty())
{
return 0;
}
long long maxrob[n];
maxrob[1]=nums[0];
if(n==1)
{
return maxrob[1];
}
else{
maxrob[2]=(nums[0]>nums[1])?nums[0]:nums[1];
maxrob[3]=(nums[0]+nums[2]>nums[1])?nums[0]+nums[2]:nums[1];
for(int i=4; i <= n; i++){
if(maxrob[i-2]>maxrob[i-3])
{
maxrob[i]=maxrob[i-2]+nums[i-1];
}
else
{
maxrob[i]=maxrob[i-3]+nums[i-1];
}
}
return (maxrob[n]>maxrob[n-1])?maxrob[n]:maxrob[n-1];
}
}
};

第一次在leetcode上做tag为动态规划的题目,思路还不够简洁,不过效果还是达到了的。

看看其他参考解法:  

一、

这是一个更简便的方法,也是自下向上进行推演(规则是:每次包含当前元素的最大值组合都是与包含前一元素的最大值组合相比较的)

class Solution {
public:
int rob(vector<int>& nums) {
int f1, f2, i, temp;
f1 = 0;
if(nums.size()){
f2 = nums[0];
f1 = (nums.size() > 1 && nums[0] < nums[1])? nums[1] : nums[0];
for(i = 2; i < nums.size(); i++){
temp = f1;
f1 = (nums[i] + f2) > f1? (nums[i] + f2) : f1;
f2 = temp;
}
}
return f1;
}
};

二、

A[i][0]表示第i次没有抢劫,A[i][1]表示第i次进行了抢劫

即A[i+1][0] = max(A[i][0], A[i][1]).. 那么rob当前的house,只能等于上次没有rob的+money[i+1], 则A[i+1][1] = A[i][0]+money[i+1].

实际上只需要两个变量保存结果就可以了,不需要用二维数组

class Solution {
public:
int rob(vector<int> &nums) {
int best0 = 0; // 表示没有选择当前houses
int best1 = 0; // 表示选择了当前houses
for(int i = 0; i < nums.size(); i++){
int temp = best0;
best0 = max(best0, best1); // 没有选择当前houses,那么它等于上次选择了或没选择的最大值
best1 = temp + nums[i]; // 选择了当前houses,值只能等于上次没选择的+当前houses的money
}
return max(best0, best1);
}
};

三、跟我的差不多,但是有些许改进。特别是nums[2] = nums[0]+nums[2]的处理。

class Solution {
public:
int rob(vector<int> &nums) {
if(nums.empty())
{
return 0;
}
int res = 0;
int length = nums.size();
if(1 == length)
{
return nums[0];
}
if(length >= 3)
{
nums[2] = nums[0]+nums[2];
}
for(int i = 3; i < length; i++)
{
if(nums[i-2]>nums[i-3])
{
nums[i] += nums[i-2];
}
else
{
nums[i] += nums[i-3];
} }
return (nums[length-2]>nums[length-1])? nums[length-2]:nums[length-1]; }
};

  

  

 

leetcode:House Robber(动态规划dp1)的更多相关文章

  1. LeetCode总结 -- 一维动态规划篇

    这篇文章的主题是动态规划, 主要介绍LeetCode中一维动态规划的题目, 列表如下: Climbing StairsDecode WaysUnique Binary Search TreesMaxi ...

  2. LeetCode初级算法--动态规划01:爬楼梯

    LeetCode初级算法--动态规划01:爬楼梯 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net ...

  3. Leetcode 198 House Robber 动态规划

    题意是强盗能隔个马抢马,看如何获得的价值最高 动态规划题需要考虑状态,阶段,还有状态转移,这个可以参考<动态规划经典教程>,网上有的下的,里面有大量的经典题目讲解 dp[i]表示到第i匹马 ...

  4. &lbrack;LeetCode&rsqb; House Robber 打家劫舍

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

  5. &lbrack;LeetCode&rsqb; House Robber III 打家劫舍之三

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  6. &lbrack;LeetCode&rsqb; House Robber II 打家劫舍之二

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

  7. LeetCode House Robber III

    原题链接在这里:https://leetcode.com/problems/house-robber-iii/ 题目: The thief has found himself a new place ...

  8. LeetCode House Robber

    原题链接在这里:https://leetcode.com/problems/house-robber/ 题目: You are a professional robber planning to ro ...

  9. LeetCode -- Word Break 动态规划,详细理解

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

随机推荐

  1. MySQL 5&period;6 主从复制如何处理——触发器&comma;函数&comma;存储过程&comma;调度事件

      截图来自MySQL5.6的pdf版文档. 说明: 1)基于语句的复制时,trigger会在slave上执行,所以slave上也需要有trigger的定义,不然会导致主从数据不一致的: 2)基于行的 ...

  2. &period;NET笔记&lpar;一&rpar;

    物理路径 context.Server.MapPath() 获取DataTable的某个单元格的值 tb.Rows[i][j] 或 tb.Rows["某一行"]["某一列 ...

  3. Html的基本元素&lpar;Element&rpar;

    本人写这篇文章是我在IT修真园里学习了一段时间,反过来复习时整理的.虽然只是些基础知识内容,希望能帮到大家. 首先我们要了解所谓的html它的定义是什么? [html:超文本标记语言,文本:txt格式 ...

  4. linux新手向-文件的权限及修改

    如果访问或执行一个文件显示Permission deny,一般是权限问题. 使用"ls -l"可以查看该目录下文件的详细信息. 1.读懂权限 第一列就是权限信息,形如: drwxr ...

  5. 使用checkstyle来规范你的项目

    Checkstyle是什么 自从做了程序员,关于格式化的讨论就不曾中断过,到底什么才是正确的,什么才是错误的,到现在也没有完整的定论.但随着时间发展,渐渐衍生出一套规范出来.没有什么绝对的正确和错误, ...

  6. 015-Go 数据库操作注意事项

    1.Query.Exec(1)Exec(update.insert.delete等无结果集返回的操作)调用完后会自动释放连接:(2)Query(返回sql.Rows)则不会释放连接,调用完后仍然占有连 ...

  7. 8&period;15 自定义tr行 滚动 信息行的滚动

    <table class="zixun-con-table"> <tr class="hover"> <th style=&quo ...

  8. python操作数据库-数据表

    数据表: 数据类型: 帮助的三种形式: 在cmd中输入: help 要帮助的主题词,或 ? 要帮助的主题词 或  \h 要帮助的主题词 . 数据表的创建: CREATE database IF NOT ...

  9. ArrayList源码中EMPTY&lowbar;ELEMENTDATA和DEFAULTCAPACITY&lowbar;EMPTY&lowbar;ELEMENTDATA的区别

    2018年7月22日09:54:17 JDK 1.8.0_162 ArrayList源码中EMPTY_ELEMENTDATA和DEFAULTCAPACITY_EMPTY_ELEMENTDATA的区别 ...

  10. VMware vSphere学习之手动克隆虚拟机

    VMware ESxi5.0中,在没有安装VMware vCenter server虚拟机管理器的情况下,vSphere Client是没有提供克隆选项的. 但是还是有以下方法可以通过vSphere ...