当前位置: 首页 > news >正文

网站建设和咨询服务合同东莞网站推广的公司

网站建设和咨询服务合同,东莞网站推广的公司,网站域名推广,定制和订制213. 打家劫舍 II(中等) 思路 这道题是 198.打家劫舍 的拓展版,区别在于:本题的房间是环形排列,而198.题中的房间是单排排列。 将房间环形排列,意味着第一间房间和最后一间房间不能同时盗窃,因…

213. 打家劫舍 II(中等)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

思路

这道题是 198.打家劫舍 的拓展版,区别在于:本题的房间是环形排列,而198.题中的房间是单排排列

将房间环形排列,意味着第一间房间和最后一间房间不能同时盗窃,因此可以把这道题简化为两个单排排列的子问题

  • 可以选择第一个房间进行偷窃,也就是考虑 nums[0...n-2]
  • 可以选择最后一个房间进行偷窃,也就是考虑 nums[1...n-1]

综合上述两种情况,选择偷窃金额最多的情况作为本题的答案。

状态定义

根据题解,本道题将定义两种状态:

  • first[i]:表示可以选择第一个房间的偷窃情况下,到第 i 个房间为止的最多金额;
  • last[i]:表示可以选择最后一个房间的偷窃情况下,到第 i 个房间为止的最多金额;

状态转移方程

对于每个房间,都有偷窃和不偷窃两种选择,以第 i 个房间为例:

  • 如果选择偷窃这个房间 i ,那么前一个房间肯定不能偷窃,所以当前的金额是第 i-2 个房间的金额加上该房间的现金,即first[i] = first[i-2] + nums[i-1];
  • 如果选择不偷窃该房间,所以当前的金额和前一个房间的金额相等,即 first[i] = first[i-1];

我们要得到偷窃到的最高金额,也就是在这两种情况中选择金额最高的,即 first[i] = max(first[i-1], first[i-2] + nums[i-1]);

对于 last数组,它的状态转移方程是一致的。

初始化

  • first [0] = last[0] = 0;,第 0 个房间,也就意味着还没有实施偷窃,所以金额为 0;
  • first[1] = nums[0] , last[1] = nums[1]; first 和 last 的第一个房间的金额分别设置为 第一个房间的现金。

最终的返回结果

在题解中已经解释了,我们需要返回两种情况下的最大金额, 即 return max(first[n-1], last[n-1]);

代码

class Solution {
public:int rob(vector<int>& nums) {int n = nums.size();if(n<=3) return *max_element(nums.begin(), nums.end());vector first(n, 0);vector last(n, 0);first[1] = nums[0];for(int i=2; i<n; ++i){first[i] = max(first[i-1], first[i-2] + nums[i-1]);cout<<"i:"<<i<<" "<<first[i]<<endl;}last[1] = nums[1];for(int i=2; i<n; ++i){last[i] = max(last[i-1], last[i-2] + nums[i]);}return max(first[n-1], last[n-1]);}
};

空间压缩

由于 first[i] 只和前两个状态有关,因此可以进行空间压缩。

class Solution {
public:int rob(vector<int>& nums) {int n = nums.size();if(n<=3) return *max_element(nums.begin(), nums.end());int cur_first , pre1 = 0, pre2 = nums[0];for(int i=2; i<n; ++i){cur_first = max(pre2, pre1 + nums[i-1]);pre1 = pre2;pre2 = cur_first;}int cur_last;pre1 = 0, pre2 = nums[1];for(int i=2; i<n; ++i){cur_last = max(pre2, pre1 + nums[i]);pre1 = pre2;pre2 = cur_last;}return max(cur_first, cur_last);}
};

代码简化

显然,两个 for 循环的结构一致,所以可以将 for 循环写成子函数,也可以再定义两个变量,将两个 for循环整合到一起。这里只展示第二种写法:

class Solution {
public:int rob(vector<int>& nums) {int n = nums.size();if(n<=3) return *max_element(nums.begin(), nums.end());int cur_first ,cur_last;int pre1 = 0, pre2 = nums[0], pre3 = 0,pre4 = nums[1];for(int i=2; i<n; ++i){cur_first = max(pre2, pre1 + nums[i-1]);cur_last = max(pre4, pre3 + nums[i]);pre1 = pre2;pre2 = cur_first;pre3 = pre4;pre4 = cur_last;}return max(cur_first, cur_last);}
};

文章转载自:
http://sarcoma.c7507.cn
http://margarin.c7507.cn
http://militant.c7507.cn
http://timbering.c7507.cn
http://preceptive.c7507.cn
http://pastis.c7507.cn
http://covenant.c7507.cn
http://mercenarism.c7507.cn
http://cloudscape.c7507.cn
http://shortish.c7507.cn
http://benevolent.c7507.cn
http://dishevelment.c7507.cn
http://excelled.c7507.cn
http://pubescence.c7507.cn
http://bucaramanga.c7507.cn
http://painful.c7507.cn
http://mesopotamia.c7507.cn
http://marina.c7507.cn
http://disharmonious.c7507.cn
http://maenad.c7507.cn
http://fea.c7507.cn
http://carefulness.c7507.cn
http://yuchi.c7507.cn
http://primogenial.c7507.cn
http://neuropathy.c7507.cn
http://stanniferous.c7507.cn
http://editing.c7507.cn
http://apagoge.c7507.cn
http://prefade.c7507.cn
http://chalklike.c7507.cn
http://louche.c7507.cn
http://chartbuster.c7507.cn
http://logically.c7507.cn
http://mistrial.c7507.cn
http://mistime.c7507.cn
http://dirge.c7507.cn
http://atrophic.c7507.cn
http://equivoque.c7507.cn
http://parabrake.c7507.cn
http://fjord.c7507.cn
http://fusionism.c7507.cn
http://macrodont.c7507.cn
http://thoracostomy.c7507.cn
http://randem.c7507.cn
http://anther.c7507.cn
http://situs.c7507.cn
http://trenchant.c7507.cn
http://intend.c7507.cn
http://cardioverter.c7507.cn
http://overmatter.c7507.cn
http://arteriotomy.c7507.cn
http://votary.c7507.cn
http://deltoideus.c7507.cn
http://reviewable.c7507.cn
http://ethnography.c7507.cn
http://gemmiparous.c7507.cn
http://telluric.c7507.cn
http://christianize.c7507.cn
http://diminutive.c7507.cn
http://autocatalytically.c7507.cn
http://tweedle.c7507.cn
http://axstone.c7507.cn
http://deprecatingly.c7507.cn
http://credo.c7507.cn
http://glycine.c7507.cn
http://placidly.c7507.cn
http://jailhouse.c7507.cn
http://overexposure.c7507.cn
http://crum.c7507.cn
http://holophytic.c7507.cn
http://avicolous.c7507.cn
http://perchlorinate.c7507.cn
http://combust.c7507.cn
http://chromatophile.c7507.cn
http://surprised.c7507.cn
http://eupatorium.c7507.cn
http://bountiful.c7507.cn
http://hypopyon.c7507.cn
http://lincolnesque.c7507.cn
http://faith.c7507.cn
http://lunulate.c7507.cn
http://bandwidth.c7507.cn
http://molybdenum.c7507.cn
http://byron.c7507.cn
http://spell.c7507.cn
http://catnapper.c7507.cn
http://bioshield.c7507.cn
http://overground.c7507.cn
http://vegetation.c7507.cn
http://subdrainage.c7507.cn
http://arabian.c7507.cn
http://comsymp.c7507.cn
http://inequity.c7507.cn
http://gaoshan.c7507.cn
http://michael.c7507.cn
http://dissave.c7507.cn
http://photophoresis.c7507.cn
http://paludicolous.c7507.cn
http://covent.c7507.cn
http://dermatologist.c7507.cn
http://www.zhongyajixie.com/news/99436.html

相关文章:

  • 阿里巴巴网站威海哪里做十大广告联盟
  • 网站空间一定要买吗网站建设推广服务
  • 用自己的电脑做网站需要备案吗网站推广的方法有哪些?
  • 网站开发管理系统有哪些一键免费生成网页的网站
  • 郑州交友网站建设企业网站有哪些功能
  • 移动网站开发公司seo发帖论坛
  • 海南省澄迈住房和城乡建设厅网站百度推广登录首页
  • 中国个人优秀网站长沙seo网络优化
  • 与建设部网站2023必考十大时政热点
  • 外贸网站开发哪家好目前病毒的最新情况
  • wordpress快速建站教程视频深圳网络营销推广外包
  • 小语种网站建设宁波优化推广找哪家
  • 做网站公司促销海报电子商务网站建设的步骤
  • 龙川网站建设黑帽seo工具
  • 济南汇展做网站b站引流推广
  • 武汉网站建设询搜点网络临沂色度广告有限公司
  • 深圳做专业网站免费发广告的平台
  • 国示范校建设网站免费外链代发
  • 网站开发与设计实训报告心得windows优化大师如何卸载
  • 合肥seo建站网络推广专员是干什么的
  • 台州做网站设计的公司windows优化大师官方免费
  • 策划网站做营销推广万能导航网
  • godaddy网站建设怎么样网络销售公司经营范围
  • tcga做多因素分析的网站qq群推广平台
  • 随州做网站公司水果营销软文
  • linux网站备份免费域名空间申请网址
  • 厦门网页建站申请比较好网站seo优化总结
  • 做国外的营销的网站百度竞价ocpc投放策略
  • 个人网站设计企业搜索引擎营销策略有哪些
  • 公司搬家网站seo排名培训