198. House Robber,213. House Robber II
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.
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
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的更多相关文章
- leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)
House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...
- 【LeetCode】213. House Robber II
House Robber II Note: This is an extension of House Robber. After robbing those houses on that stree ...
- [LeetCode] 213. House Robber II 打家劫舍之二
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- [LeetCode] 213. House Robber II 打家劫舍 II
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
- 【LeetCode】213. House Robber II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/house-rob ...
- 【刷题-LeetCode】213. House Robber II
House Robber II You are a professional robber planning to rob houses along a street. Each house has ...
- 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 ...
- 213. House Robber II
题目: Note: This is an extension of House Robber. After robbing those houses on that street, the thief ...
- LeetCode 213. House Robber II
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
随机推荐
- 基础命名空间:序列化 System.Runtime.Serialization
对象通常都有状态(state),从一个对象中抽取这种状态,不论是将它存储于某地,还是通过网络传送,这种抽取动作称为“将一个对象序列化”,而反向处理过程,从一个被序列化的状态重建一个对象即为反序列化. ...
- Winform中子线程访问界面控件时被阻塞解决方案
public partial class WebData_Import : Form { //声明用于访问主界面的委托类型 public delegate void deleGetOrderdata( ...
- iOS 面试题 3
0.请写出代码,用blocks来取代上例中的protocol,并比较两种方法的优势.实际应用部分?请写出代码,用blocks取代协议或回调方法 声明: #import <Foundation/F ...
- UIScrollView 代理方法
在使用UIScrollView和它的子类UITableView时,有时需要在不同操作状态下,做不同的响应. 如何截获这些状态,如正在滚动,滚动停止等,使用UIScrollViewDelegate_Pr ...
- (原)Microsoft Source Reader的简单使用
感觉Microsoft Source Reader还是比较坑的,只是由于需要,不得不使用.其实按照Microsoft提供的示例,基本上可以正常的调试出程序来. 下面的例子,简单的给出了Source R ...
- ExtJs 学习笔记
1.显示中文 <script type="text/javascript" src="../../locale/ext-lang-zh_CN.js"&g ...
- jQuery常用选择器汇总
一.基本选择器 <body> <div> <div id="div1"> aaaaaaaaaaa</div> <div cla ...
- 蘑菇街2015校招技术类笔试题A卷,回忆版(杭州站)
笔试时间:10月9号 下午 1.一串数据的最大递增序列,输出个数 例如 4,2, 6,3, 1,5, 最大递增序列为, 2,3, 5,输出3, 2.求两个整型数据集合的交集,尽可能少用时间. 假设两个 ...
- 镜像树(dfs)
1214: J.镜像树 时间限制: 1 Sec 内存限制: 64 MB提交: 18 解决: 7 标签提交统计讨论版 题目描述 一棵二叉树,若其与自己的镜像完全相同,就称其为镜像树(即这棵二叉树关于 ...
- KL25用SPI操作nor flash
KL25的SPI连接一个nor flash.该flash型号为FM25F04,支持SPI的模式0和模式3,要求高位先发送,在上升沿采集数据. 通常,SPI有4种模式,取决于CPOL与CPHA如何配置. ...