网站ip地址 转向域名,宁波外贸网站设计公司,扬中门户,上海网站建设公司电话关于此题我的往期文章,动规五部曲详解篇#xff1a;
leetCode 213. 打家劫舍 II 动态规划 房间连成环怎么偷呢#xff1f;_呵呵哒(#xffe3;▽#xffe3;)的博客-CSDN博客https://heheda.blog.csdn.net/article/details/133409962213. 打家劫舍 II - 力扣#x…关于此题我的往期文章,动规五部曲详解篇
leetCode 213. 打家劫舍 II 动态规划 房间连成环怎么偷呢_呵呵哒(▽)的博客-CSDN博客https://heheda.blog.csdn.net/article/details/133409962213. 打家劫舍 II - 力扣LeetCode 你是一个专业的小偷计划偷窃沿街的房屋每间房内都藏有一定的现金。这个地方所有的房屋都 围成一圈 这意味着第一个房屋和最后一个房屋是紧挨着的。同时相邻的房屋装有相互连通的防盗系统如果两间相邻的房屋在同一晚上被小偷闯入系统会自动报警 。
给定一个代表每个房屋存放金额的非负整数数组计算你 在不触动警报装置的情况下 今晚能够偷窃到的最高金额 左闭右闭
1回溯 198. 打家劫舍 - 力扣LeetCode
class Solution {
public:int rob(vectorint nums) {int n nums.size();functionint(int)dfs [](int i) - int {if(i0) return 0;return max(dfs(i-1),dfs(i-2)nums[i]);};return dfs(n-1);}
};
超时
2递归搜索 保存计算结果 记忆化搜索
class Solution {
public:// 记忆化递归int rob(vectorint nums) {int n nums.size();vectorint memo(n,-1);functionint(int)dfs [](int i) - int {if(i0) return 0;int res memo[i];if(res ! -1) return res;return resmax(dfs(i-1),dfs(i-2)nums[i]);};return dfs(n-1);}
};
把 198.打家劫舍 的函数改成 robRange
class Solution {
public:int robRange(vectorint nums,int start,int end) {// int nend-start1;// vectorint memo(n1,-1);vectorint memo(end1,-1);functionint(int)dfs [](int i) - int {if(istart) return 0;int res memo[i];if(res ! -1) return res;return resmax(dfs(i-1),dfs(i-2)nums[i]);};return dfs(end);}int rob(vectorint nums) {int n nums.size();if (n 0) return 0;if (n 1) return nums[0];int result1 robRange(nums, 0, n - 2); // 情况二int result2 robRange(nums, 1, n - 1); // 情况三return max(result1, result2);}
};
31:1 翻译成递推
① 1:1 翻译成递推
class Solution {
public:// 1:1 翻译成递推f[i] max(f[i-1],f[i-2]nums[i]);int rob(vectorint nums) {int n nums.size();vectorint f(n,0);if(nums.size()0) return 0;if(nums.size()1) return nums[0];f[0]nums[0];f[1]max(nums[0],nums[1]);for(int i2;in;i) f[i] max(f[i-1],f[i-2]nums[i]);return f[n-1];}
};
把 198.打家劫舍 的函数改成 robRange
class Solution {
public:int robRange(vectorint nums,int start,int end) {if(end start) return nums[start];// int n nums.size();int n end1;vectorint f(n,0);f[start]nums[start];f[start1]max(nums[start],nums[start1]);for(int istart2;iend;i) f[i] max(f[i-1],f[i-2]nums[i]);return f[end];}int rob(vectorint nums) {int n nums.size();if (n 0) return 0;if (n 1) return nums[0];int result1 robRange(nums, 0, n - 2); // 情况二int result2 robRange(nums, 1, n - 1); // 情况三return max(result1, result2);}
};
② 1:1 翻译成递推
class Solution {
public:// 1:1 翻译成递推f[i2] max(f[i1],f[i]nums[i]);int rob(vectorint nums) {int n nums.size();vectorint f(n2,0);for(int i0;in;i) f[i2] max(f[i1],f[i]nums[i]);return f[n1];}
};
把 198.打家劫舍 的函数改成 robRange
class Solution {
public:/*int robRange(vectorint nums,int start,int end) {if(end start) return nums[start];int n nums.size();vectorint f(n2,0);for(int istart;in;i) f[i2] max(f[i1],f[i]nums[i]);return f[end2];}*/int robRange(vectorint nums,int start,int end) {// int n nums.size();int nend1;vectorint f(n2,0);for(int istart;iend;i) f[i2] max(f[i1],f[i]nums[i]);return f[end2];}int rob(vectorint nums) {int n nums.size();if (n 0) return 0;if (n 1) return nums[0];int result1 robRange(nums, 0, n - 2); // 情况二int result2 robRange(nums, 1, n - 1); // 情况三return max(result1, result2);}
};
4优化空间复杂度 class Solution {
public:// 空间优化int rob(vectorint nums) {int n nums.size();if(n0) return 0;if(n1) return nums[0];int f0nums[0];int f1max(nums[0],nums[1]);for(int i2;in;i) {int new_f max(f1,f0nums[i]);f0f1;f1new_f;}return f1;}
};
把 198.打家劫舍 的函数改成 robRange
class Solution {
public:int robRange(vectorint nums,int start,int end) {if(end start) return nums[start];int f0nums[start];int f1max(nums[start],nums[start1]);for(int istart2;iend;i) {int new_f max(f1,f0nums[i]);f0f1;f1new_f;}return f1;}int rob(vectorint nums) {int n nums.size();if (n 0) return 0;if (n 1) return nums[0];int result1 robRange(nums, 0, n - 2); // 情况二int result2 robRange(nums, 1, n - 1); // 情况三return max(result1, result2);}
}; class Solution {
public: // 空间优化int rob(vectorint nums) {int n nums.size();int f00,f10;for(const int x:nums) {int new_f max(f1, f0 x);f0 f1;f1 new_f;}return f1;}
};
class Solution {
public:int robRange(vectorint nums,int start,int end) {int f00,f10;for(int istart;iend;i) {int new_fmax(f1,f0nums[i]);f0f1;f1new_f;}return f1;}int rob(vectorint nums) {int n nums.size();if (n 0) return 0;if (n 1) return nums[0];int result1 robRange(nums, 0, n - 2); // 情况二int result2 robRange(nums, 1, n - 1); // 情况三return max(result1, result2);}
};
我的往期文章推荐
leetCode 213. 打家劫舍 II 动态规划 房间连成环怎么偷呢_呵呵哒(▽)的博客-CSDN博客https://heheda.blog.csdn.net/article/details/133409962leetCode 198.打家劫舍 动态规划入门:从记忆化搜索到递推-CSDN博客https://blog.csdn.net/weixin_41987016/article/details/134179583?spm1001.2014.3001.5501leetCode 198.打家劫舍 动态规划 优化空间复杂度_呵呵哒(▽)的博客-CSDN博客https://blog.csdn.net/weixin_41987016/article/details/133390944?spm1001.2014.3001.5501 文章转载自: http://www.morning.lkgqb.cn.gov.cn.lkgqb.cn http://www.morning.tpdg.cn.gov.cn.tpdg.cn http://www.morning.wkhfg.cn.gov.cn.wkhfg.cn http://www.morning.mhlsx.cn.gov.cn.mhlsx.cn http://www.morning.fwlch.cn.gov.cn.fwlch.cn http://www.morning.rlzxr.cn.gov.cn.rlzxr.cn http://www.morning.mhlkc.cn.gov.cn.mhlkc.cn http://www.morning.kndst.cn.gov.cn.kndst.cn http://www.morning.bmjfp.cn.gov.cn.bmjfp.cn http://www.morning.hxxyp.cn.gov.cn.hxxyp.cn http://www.morning.ftrpvh.cn.gov.cn.ftrpvh.cn http://www.morning.zfgh.cn.gov.cn.zfgh.cn http://www.morning.mwbqk.cn.gov.cn.mwbqk.cn http://www.morning.pwmpn.cn.gov.cn.pwmpn.cn http://www.morning.rczrq.cn.gov.cn.rczrq.cn http://www.morning.lrskd.cn.gov.cn.lrskd.cn http://www.morning.bryyb.cn.gov.cn.bryyb.cn http://www.morning.wlbwp.cn.gov.cn.wlbwp.cn http://www.morning.lwtfx.cn.gov.cn.lwtfx.cn http://www.morning.xpzgg.cn.gov.cn.xpzgg.cn http://www.morning.mzcsp.cn.gov.cn.mzcsp.cn http://www.morning.ngmjn.cn.gov.cn.ngmjn.cn http://www.morning.tqsmg.cn.gov.cn.tqsmg.cn http://www.morning.gmysq.cn.gov.cn.gmysq.cn http://www.morning.grcfn.cn.gov.cn.grcfn.cn http://www.morning.xltwg.cn.gov.cn.xltwg.cn http://www.morning.lhgkr.cn.gov.cn.lhgkr.cn http://www.morning.yrnrr.cn.gov.cn.yrnrr.cn http://www.morning.jwbnm.cn.gov.cn.jwbnm.cn http://www.morning.qfbzj.cn.gov.cn.qfbzj.cn http://www.morning.rtbx.cn.gov.cn.rtbx.cn http://www.morning.saastob.com.gov.cn.saastob.com http://www.morning.bfhrj.cn.gov.cn.bfhrj.cn http://www.morning.brkrt.cn.gov.cn.brkrt.cn http://www.morning.gdljq.cn.gov.cn.gdljq.cn http://www.morning.tqxtx.cn.gov.cn.tqxtx.cn http://www.morning.zlrrj.cn.gov.cn.zlrrj.cn http://www.morning.jzbjx.cn.gov.cn.jzbjx.cn http://www.morning.wqwbj.cn.gov.cn.wqwbj.cn http://www.morning.dgng.cn.gov.cn.dgng.cn http://www.morning.mfnjk.cn.gov.cn.mfnjk.cn http://www.morning.mwkwg.cn.gov.cn.mwkwg.cn http://www.morning.nhzxr.cn.gov.cn.nhzxr.cn http://www.morning.cpnlq.cn.gov.cn.cpnlq.cn http://www.morning.pxjp.cn.gov.cn.pxjp.cn http://www.morning.ntgrn.cn.gov.cn.ntgrn.cn http://www.morning.nicetj.com.gov.cn.nicetj.com http://www.morning.nrydm.cn.gov.cn.nrydm.cn http://www.morning.zbnts.cn.gov.cn.zbnts.cn http://www.morning.mqldj.cn.gov.cn.mqldj.cn http://www.morning.lbbgf.cn.gov.cn.lbbgf.cn http://www.morning.tgczj.cn.gov.cn.tgczj.cn http://www.morning.wwdlg.cn.gov.cn.wwdlg.cn http://www.morning.bhpsz.cn.gov.cn.bhpsz.cn http://www.morning.wrdlf.cn.gov.cn.wrdlf.cn http://www.morning.xqqcq.cn.gov.cn.xqqcq.cn http://www.morning.sgfpn.cn.gov.cn.sgfpn.cn http://www.morning.ywqw.cn.gov.cn.ywqw.cn http://www.morning.bpmdz.cn.gov.cn.bpmdz.cn http://www.morning.myzfz.com.gov.cn.myzfz.com http://www.morning.wtnwf.cn.gov.cn.wtnwf.cn http://www.morning.fnkcg.cn.gov.cn.fnkcg.cn http://www.morning.yltyz.cn.gov.cn.yltyz.cn http://www.morning.jltmb.cn.gov.cn.jltmb.cn http://www.morning.tongweishi.cn.gov.cn.tongweishi.cn http://www.morning.hphqy.cn.gov.cn.hphqy.cn http://www.morning.kztpn.cn.gov.cn.kztpn.cn http://www.morning.lxwjx.cn.gov.cn.lxwjx.cn http://www.morning.mqbzk.cn.gov.cn.mqbzk.cn http://www.morning.lzzqz.cn.gov.cn.lzzqz.cn http://www.morning.tkkjl.cn.gov.cn.tkkjl.cn http://www.morning.zgnng.cn.gov.cn.zgnng.cn http://www.morning.pwksz.cn.gov.cn.pwksz.cn http://www.morning.wxckm.cn.gov.cn.wxckm.cn http://www.morning.nwcgj.cn.gov.cn.nwcgj.cn http://www.morning.bpmfz.cn.gov.cn.bpmfz.cn http://www.morning.ggtgl.cn.gov.cn.ggtgl.cn http://www.morning.mqxrx.cn.gov.cn.mqxrx.cn http://www.morning.hnhgb.cn.gov.cn.hnhgb.cn http://www.morning.nkmw.cn.gov.cn.nkmw.cn