198. House Robber,213. House Robber II

时间:2022-12-21 19:46:57

198. House Robber

Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy

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.

 
class Solution {
public:
int rob(vector<int>& nums) {
int nums_size = nums.size();
int max_money = ; vector<int>dp(nums_size,); for(int i=;i<nums_size;++i){
int m = ;
for(int j=i-;j>=;j--){
m = max(m,dp[j]);
}
dp[i] = m+nums[i];
max_money = max(max_money,dp[i]);
}
return max_money;
} }; /**
dp[i] = max(dp[i-2],dp[i-3]...dp[1])+nums[i];
[9,8,9,20,8]
[1,2,3,55,54,2]
*/
 
 

213. House Robber II

Total Accepted: 18274 Total Submissions: 63612 Difficulty: Medium

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 are arranged 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.

/**
dp[i] = max(dp[i-2],dp[i-3]...dp[1])+nums[i];
[9,8,9,20,8]
[1,2,3,55,54,2]
*/
class Solution {
public:
int rob(vector<int>& nums,int start,int end) {
if(end<=start) return ;
int nums_size = end-start;
int max_money = ; vector<int>dp(nums_size,);
int k = ;
cout<<"start="<<start<<" end="<<endl;
for(int i=start;i<end;++i){
int m = ;
for(int j=k-;j>=;j--){
m = max(m,dp[j]);
}
dp[k] = m+nums[i];
cout<<"dp["<<(k)<<"]="<<dp[k]<<endl;
max_money = max(max_money,dp[k]);
k++;
} return max_money;
}
int rob(vector<int>& nums) {
int nums_size = nums.size();
return nums_size== ? nums[] : max(rob(nums,,nums_size-),rob(nums,,nums_size));
}
};

198. House Robber,213. House Robber II的更多相关文章

  1. leetcode 198&period; House Robber 、 213&period; House Robber II 、337&period; House Robber III 、256&period; Paint House&lpar;lintcode 515&rpar; 、265&period; Paint House II&lpar;lintcode 516&rpar; 、276&period; Paint Fence&lpar;lintcode 514&rpar;

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

  2. 【LeetCode】213&period; House Robber II

    House Robber II Note: This is an extension of House Robber. After robbing those houses on that stree ...

  3. &lbrack;LeetCode&rsqb; 213&period; House Robber II 打家劫舍之二

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

  4. &lbrack;LeetCode&rsqb; 213&period; House Robber II 打家劫舍 II

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

  5. 【LeetCode】213&period; House Robber II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/house-rob ...

  6. 【刷题-LeetCode】213&period; House Robber II

    House Robber II You are a professional robber planning to rob houses along a street. Each house has ...

  7. Java for LeetCode 213 House Robber II

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

  8. 213&period; House Robber II

    题目: Note: This is an extension of House Robber. After robbing those houses on that street, the thief ...

  9. LeetCode 213&period; House Robber II

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

随机推荐

  1. storyboard中的三种传值

    三种传值:属性传值 block传值 以及 代理传值 (这里我用前面的页面和后面的)来表示两个控制器:LoginViewController和RegisterViewController 建立两个控制器 ...

  2. photoshop几个基本技巧

    原文地址:http://blog.thmz.com/user1/936/archives/2008/20418.htm 去除文字的几种方法: 1.访印图章工具 2.修补工具 3.修复画笔工具 4.画笔 ...

  3. &lpar;C&num; &rpar; 解析XML。

    解析XML有很多方法,之前用专门写的XMLProcess 或XMLHelper 解析类.其实有个较简单的解析就是用Linq查询. 例如有如下XML <?xml version="1.0 ...

  4. SimpleHttpServer的学习(1)

    闲来没事,分析一下一个简单的HttpServer github地址: https://github.com/Filirom1/SimpleHTTPServer 实现的功能很简单就是一个FTP服务器 默 ...

  5. centos7创建新用户

    创建新用户 创建一个叫xiaoming的用户: [root@192 ~]# adduser xiaoming 为这个用户初始化密码,linux会判断密码复杂度,不过可以强行忽略: [root@192 ...

  6. RxSwift&lpar;一&rpar;

    文/iOS_Deve(简书作者) 原文链接:http://www.jianshu.com/p/429b5160611f 著作权归作者所有,转载请联系作者获得授权,并标注"简书作者" ...

  7. Java&plus;Maven&plus;selenium&plus;testing&plus;reportNG自动化测试框架

    最近公司新出了一个产品,需要搭建自动化测试框架,这是一个学以至用的好机会,跟上级申请后,决定搭建一个java自动化测试框架. Java自动化测试对我来讲可以说不难不易,因为java是我大学在校四年学的 ...

  8. maven jdk 版本配置

    一种是配置 pom.xml,一种是配置 settings.xml. 方式一:settings.xml 配置 打开 %maven%/conf/settings.xml 文件并编辑它(%maven% 表示 ...

  9. wxpython安装,demo下载

    wxPython介绍      wxPython是Python语言的一套优秀的GUI图形库.wxPython可以很方便的创建完整的.功能键全的GUI用户界面. wxPython安装 本安装采用pip自 ...

  10. mysql show master status为空值

    问题 执行show master status,输出结果为空: mysql> show master status; Empty set (0.00 sec) 原因 mysql没有开启日志. 查 ...