[leetcode] 213. House Robber II 解题报告
题目链接:https://leetcode.com/problems/house-robber-ii/
Note: This is an extension of House Robber.
After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place arearranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.
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.
思路:和House Robber题目类似,只是这次有了环形,那么我们可以做两次动归,第一次不抢第一家的钱,则可以抢最后一家的钱。第二次抢第一家的钱,然后就不可以抢最后一家的钱,因此最后把各自最大值比较一下,返回最大的一个即是能够抢到的最多的钱。
时间复杂度为O(n),空间复杂度为O(n)。
代码如下:
class Solution {
public:int rob(vector<int>& nums) {if(nums.size()==0) return 0;if(nums.size()==1) return nums[0];int len = nums.size();vector<int> dp1(len+1, 0), dp2(len+1, 0);dp1[1] = nums[0];for(int i=1; i<len-1;i++) dp1[i+1] = max(dp1[i], dp1[i-1]+nums[i]);for(int i=1; i<len; i++) dp2[i+1] = max(dp2[i], dp2[i-1]+nums[i]);return max(dp1[len-1], dp2[len]);}
};
[leetcode] 213. House Robber II 解题报告相关推荐
- LeetCode 167.Two Sum II 解题报告
LeetCode 167.Two Sum II 解题报告 题目描述 Given an array of integers that is already sorted in ascending ord ...
- LeetCode 213. House Robber II(小偷游戏)
原题网址:https://leetcode.com/problems/house-robber-ii/ Note: This is an extension of House Robber. Afte ...
- 【LeetCode】275. H-Index II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/h-index- ...
- LeetCode 213 House Robber II Python
题意:你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金.这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的.同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的 ...
- leetcode 213. House Robber II | 213. 打家劫舍 II(Java)
题目 https://leetcode.com/problems/house-robber-ii/ 题解 这道题是「198. 打家劫舍」的进阶,和第 198 题的不同之处是,这道题中的房屋是首尾相连的 ...
- [Leetcode] 212. Word Search II 解题报告
题目: Given a 2D board and a list of words from the dictionary, find all words in the board. Each word ...
- [LeetCode]844. Backspace String Compare 解题报告(C++)
[LeetCode]844. Backspace String Compare 解题报告(C++) 题目描述 Given two strings S and T, return if they are ...
- 【LeetCode】91. Decode Ways 解题报告(Python)
[LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- 【LeetCode】House Robber I II 解题报告
[题目] I You are a professional robber planning to rob houses along a street. Each house has a certain ...
最新文章
- T75 大数加法+取模
- 最好用的货币:货币发展史和BCH
- 快速查找所有存储过程/触发器中是否包含某个字符串
- [Mysql]——通过例子理解事务的4种隔离级别
- Kafka 监控 Kafka Eagle 精简版本
- Java JDK1.8新特性之四大函数式接口
- c语言怎么运行出星星,C语言打印星星的问题
- 把Unity的jdk环境添加到环境变量
- 安装java虚拟机_JAVA虚拟机的安装以及JAVA的环境配置
- HI3518E 中Sample Venc分析
- matlab simulink 六自由度机械臂模糊控制pid
- 计算机显示器黑屏首先检查,计算机显示器黑屏的原因是什么?电脑显示器黑屏的解决方案...
- HDOJ 5296 Annoying problem LCA+数据结构
- 网络计算机应急处理,国家计算机网络应急技术处理协调中心-计算机网络安全应急处理.ppt...
- 程序员把开发搬到云服务器,如何将IDEA开发的java web项目移植到腾讯云服务器
- 记账分享:如何记录收支明细,并分析、打印保存。
- 两化融合贯标之-申请表案例
- 《学成在线 》 网站制作源码及总结html+css
- 如何构建一个新闻搜索引擎
- java发包_【Java】UDP发包的简单实现
热门文章
- 新版WordPress网址导航主题模板+自适应WAP
- web前端学习第十四~十八天
- 罗技G402插上电脑后没反应
- Java的小数点后精度计算
- Java5、8、9章复习总结
- 短期学习目标2022/3/16
- conda安装packages时报错File “C:\ProgramData\anaconda3\lib\site-packages\conda\core\subdir_data.py“
- 微信开通状态检测工具(免验证码版)运行原理
- Human-like learning在对话机器人中的魔性运用
- 微信小游戏-飞机游戏玩法改造系列(二:支持血条)