https://leetcode.com/problems/house-robber/

题意:

一维数组,相加不相邻的数组,返回最大的结果。

思路:

一开始思路就是DP,用一维数组保存dp[i]保存如果偷第i间,此时可偷到多少。DP的方向不太好,所以效率很低。

Runtime: 4 ms, faster than 17.53%

class Solution
{
public:
int rob(vector<int> &nums)
{
int res = ;
int len = nums.size();
if (len <= )
return res;
int dp[len];
for (int i = ; i < len; i++)
{
dp[i] = nums[i];
res = max(res, dp[i]);
}
if (len > )
{
dp[] += dp[];
res = max(res, dp[]);
}
for (int j = ; j < len; j++)
{
dp[j] += max(dp[j - ], dp[j - ]);
res = max(res, dp[j]);
}
return res;
}
};

后面DP思路改成:dp[i]记录在偷到第i位时,最大可偷多少钱。

可偷最多的钱要么是偷这次的,要么是不偷这一次的。

转移方程为: dp[i]=max(dp[i-]+nums[i],dp[i-])

Runtime: 0 ms, faster than 100.00%

class Solution
{
public:
int rob(vector<int> &nums)
{
int res = ;
int len = nums.size();
if (len <= )
return res;
if (len == )
return nums[];
if (len == )
return (nums[] > nums[] ? nums[] : nums[]);
int dp[len];
dp[] = nums[];
dp[] = max(nums[], nums[]); for (int i = ; i < len; i++)
{
dp[i] = max(dp[i - ] + nums[i], dp[i - ]);
}
return dp[len - ];
}
};

leetcode 198. House Robber (Easy)的更多相关文章

  1. leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)

    House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...

  2. (easy)LeetCode 198.House Robber

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

  3. [LeetCode] 198. House Robber 打家劫舍

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

  4. Leetcode 198 House Robber

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

  5. Java for LeetCode 198 House Robber

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

  6. Java [Leetcode 198]House Robber

    题目描述: You are a professional robber planning to rob houses along a street. Each house has a certain ...

  7. [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 ...

  8. Leetcode 198 House Robber 动态规划

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

  9. leetcode 198 House Robber I

    function rob(nums) { if(!nums || nums.length === 0) { return 0; } else if(nums.length < 2){ retur ...

随机推荐

  1. 预处理器#include 指令

    预处理器发现 #include 指令后,就会寻找后跟的文件名并把这个文件的内容包含到当前文件中.被包含文件中的文本将替换源代码文件中的#include指令,就像你把被包含文件中的全部内容键入到源文件中 ...

  2. c# 可移动可改变大小的控件

    因为业务需要,百度了个可移动可改变大小的控件,然后自己修改了下,功能类似vs的设计面板中的功能差不多,可拖拽,改变大小 拖动的 public class MoveControl { #region 自 ...

  3. 为什么说 2017 年你必须要学习 Go 了(多核,网络,多人协作,简单非OO,没有注解,Native,垃圾收集,代码优雅),附两个评论

    为什么要学习Go Go是未来的服务端语言— Tobias Lütke, Shopify.在过去的几年中,Golang逐步流行起来. 还有什么能比一门新语言让码农们疯狂呢? 因此,我开始学习了一段时间G ...

  4. JavaScript规定了几种语言类型?

    JavaScript中的每一个值都有它自己的类型,JavaScript规定了七种语言类型: 1.Undefined 2.Null 3.Boolean 4.String 5.Number 6.Symbo ...

  5. shell日期整理

    date 当前日期+时间 # 日期格式化:date+"" - date +"%Y%m%d" 不带横杠分隔符的日期20160107 date +"%Y% ...

  6. Python处理图片

    # -*- coding: UTF-8 -*- from PIL import Image import os import sys reload(sys) sys.setdefaultencodin ...

  7. 【Zookeeper02】ZK的作用以及使用

    上一篇介绍了ZK的安装以及集群的搭建,这只能算是个软件安装过程,具体是做什么的.怎么用也没有做解释,这一篇中博主就自己的私人理解简单写一下: 1.是什么: a.Zookeeper是一个分布式协调服务, ...

  8. nginx之gzip压缩

    nginx的gizp压缩 为了使网站节省带宽和加快访问速度,在服务器方面的一个优化的就是使用nginx提供的gzip压缩. 一.使用压缩原理: 1.当用户使用浏览器访问网站时,就是在发送一个http请 ...

  9. 长春理工大学第十四届程序设计竞赛(重现赛)I

    I.Fate Grand Order 题目链接:https://ac.nowcoder.com/acm/contest/912/I 题目: Fate Grand Order是型月社发行的角色扮演类手机 ...

  10. Windows 命令行文本操作

    Windows下文件操作,大部分的时候用的都是用Windows 资源管理器(就是双击 “我的电脑” 的时候看到的图形界面). 接下来,以Windows命令行下操作文本为例,看看命令行在操作文件方面有多 ...