【Leetcode 198】关关的刷题日记69 – Leetcode 198 House Robber

2017 年 12 月 17 日 专知 关关

点击上方“专知”,关注获取专业AI知识”

关关的刷题日记69 – Leetcode 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.

题目的意思是小偷沿着一排街道抢劫房子,每个房子都有一定数额的钱,但是不可以抢相邻的房子,问可以抢到的最大的钱数。

思路

动态规划的题目:设re[i]是抢劫前i个房子获得的最大收益,那么可以分为第i个房子不被抢和第i个房子被抢两种情况:第i个房子不被抢,re[i]=re[i-1];第i个房子被抢,re[i]=re[i-2]+nums[i];状态转移方程re[i]=max(re[i-1], re[i-2]+nums[i])。

class Solution {public:
int rob(vector<int>& nums) {
int n=nums.size();
if(n==0)
return 0;
if(n==1)
return nums[0];
if(n==2)
return max(nums[0], nums[1]);
int temp1=nums[0], temp2=max(nums[0],nums[1]), re;
for(int i=2; i<n; i++)
{
re=max(temp1+nums[i],temp2);
temp1=temp2;
temp2=re;
}
return re;
}};

以上就是关关关于这道题的总结经验,希望大家能够理解,有什么问题可以在我们的专知公众号平台上交流或者加我们的QQ专知-人工智能交流群 426491390,也可以加入专知——Leetcode刷题交流群(请先加微信小助手weixinhao: Rancho_Fang)。

专知网站查看Leetcode刷题日记:


请登录www.zhuanzhi.ai或者点击阅读原文,顶端搜索“Leetcode” 主题,取查看获得专知Leetcode所有资源!如下图所示~ 


 请感兴趣的同学,扫一扫下面群二维码,加入到专知-LeetCode学习交流群!(注明 Leetcode 刷题)



欢迎转发到你的微信群和朋友圈,分享专业AI知识!


获取更多关于机器学习以及人工智能知识资料,请访问www.zhuanzhi.ai,  或者点击阅读原文,即可得到!


-END-

欢迎使用专知

专知,一个新的认知方式!专注在人工智能领域为AI从业者提供专业可信的知识分发服务, 包括主题定制、主题链路、搜索发现等服务,帮你又好又快找到所需知识。


使用方法>>访问www.zhuanzhi.ai, 或点击文章下方“阅读原文”即可访问专知

中国科学院自动化研究所专知团队

@2017 专知

专 · 知


关注我们的公众号,获取最新关于专知以及人工智能的资讯、技术、算法、深度干货等内容。扫一扫下方关注我们的微信公众号。

点击“阅读原文”,使用专知

登录查看更多
6

相关内容

LeetCode is a social platform for preparing IT technical interviews.
【DeepMind推荐】居家学习的人工智能干货资源大全集
专知会员服务
106+阅读 · 2020年6月27日
可解释强化学习,Explainable Reinforcement Learning: A Survey
专知会员服务
126+阅读 · 2020年5月14日
【IJCAI2020-CMU】结构注意力的神经抽象摘要
专知会员服务
21+阅读 · 2020年4月23日
【关关的刷题日记60】Leetcode 437. Path Sum III
【 关关的刷题日记53】 Leetcode 100. Same Tree
专知
10+阅读 · 2017年12月1日
【 关关的刷题日记47】Leetcode 38. Count and Say
【LeetCode 136】 关关的刷题日记32 Single Number
【LeetCode 500】关关的刷题日记27 Keyboard Row
专知
3+阅读 · 2017年11月5日
Arxiv
6+阅读 · 2018年7月29日
Arxiv
3+阅读 · 2018年4月5日
Arxiv
5+阅读 · 2018年3月16日
VIP会员
相关VIP内容
【DeepMind推荐】居家学习的人工智能干货资源大全集
专知会员服务
106+阅读 · 2020年6月27日
可解释强化学习,Explainable Reinforcement Learning: A Survey
专知会员服务
126+阅读 · 2020年5月14日
【IJCAI2020-CMU】结构注意力的神经抽象摘要
专知会员服务
21+阅读 · 2020年4月23日
Top
微信扫码咨询专知VIP会员