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 n = nums.size();
vector<int> f(n,0);
if(n <=0) return 0;
if(n ==1) return nums[0];
f[0] = nums[0];
f[1] = max(nums[0],nums[1]);
for(int i = 2 ; i< n;i++){
f[i] = max(f[i-1],nums[i]+ f[i-2]);
}
return f[n-1];
}
};

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的基础上分为有头和无头即可。

class Solution {
public:
int robber(vector<int> &nums,int l,int r){
int cur=0,pre=0;
for(int i=l;i<=r;i++){
int temp = max(pre + nums[i],cur);
pre = cur;
cur = temp;
}
return cur;
}
int rob(vector<int>& nums) {
int n = nums.size();
if(n <= 0) return 0;
if(n==1) return nums[0];
if(n==2) return max(nums[0],nums[1]);
return max(robber(nums,0,n-2),robber(nums,1,n-1));
}
};

337. House Robber III

The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the “root.” Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that “all houses in this place forms a binary tree”. It will automatically contact the police if two directly-linked houses were broken into on the same night.

Determine the maximum amount of money the thief can rob tonight without alerting the police.

Example 1:

     3
/ \
2 3
\ \
3 1

Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.

Example 2:

     3
/ \
4 5
/ \ \
1 3 1

Maximum amount of money the thief can rob = 4 + 5 = 9.

https://leetcode.com/discuss/91899/step-by-step-tackling-of-the-problem

非常好的进阶思路,这里贴出方法二和方法三

方法二:

class Solution {
public:
int robber(TreeNode* root,unordered_map<TreeNode*,int> &maps){
if(!root) return 0;
auto it = maps.find(root);
if(it != maps.end()) return maps[root];
int val = 0;
if(root->left) val += robber(root->left->left,maps) + robber(root->left->right,maps);
if(root->right) val += robber(root->right->left,maps) + robber(root->right->right,maps);
val = max(val+root->val,robber(root->left,maps)+robber(root->right,maps));
maps[root]= val;
return val;
}
int rob(TreeNode* root) {
unordered_map<TreeNode*,int> maps;
return robber(root,maps);
}
};

方法三:

class Solution {
public:
vector<int> robber(TreeNode* root){
if(!root) return vector<int>(2);
vector<int> left = robber(root->left);
vector<int> right = robber(root->right);
std::vector<int> res(2);
res[0] = max(left[0],left[1]) + max(right[0],right[1]);
res[1] = root->val + left[0] + right[0];
return res;
}
int rob(TreeNode* root) {
vector<int> res = robber(root);
return max(res[0],res[1]);
}
};

337. House Robber III(包含I和II)的更多相关文章

  1. 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:二叉树下的不能相邻,求能 ...

  2. Leetcode 337. House Robber III

    337. House Robber III Total Accepted: 18475 Total Submissions: 47725 Difficulty: Medium The thief ha ...

  3. [LeetCode] 337. House Robber III 打家劫舍之三

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  4. [LeetCode] 337. House Robber III 打家劫舍 III

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  5. Java [Leetcode 337]House Robber III

    题目描述: The thief has found himself a new place for his thievery again. There is only one entrance to ...

  6. 【leetcode】337. House Robber III

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  7. 337. House Robber III——树的题目几乎都是BFS、DFS,要么递归要么循环

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  8. 337. House Robber III

    二刷吧..不知道为什么house robbery系列我找不到笔记,不过印象中做了好几次了. 不是很难,用的post-order做bottom-up的运算. 对于一个Node来说,有2种情况,一种是选( ...

  9. LeetCode OJ 337. House Robber III

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

随机推荐

  1. C#之winform实现文件拖拽功能【转】

    将一个文件拖拽到窗体的某个控件时,将该控件的路径显示在该控件上,只要拿到了路径自然可以读取文件中的内容了 将一个控件的属性AllowDrop设置为true,然后添加DragDrop.DragEnter ...

  2. 2018.4.12 各个系统安装MyEclipse过程(包括Mac、Linux、Windows)

    首先下载MyEclipse 最新官网在这里http://www.myeclipsecn.com/ mac 安装 . 在安装第一步会显示 "安装myeclipse显示更低版本javase6&q ...

  3. 字符串的驻留(String Interning)

    http://www.cnblogs.com/artech/archive/2007/03/04/663728.html 关于字符串的驻留的机制,对于那些了解它的人肯定会认为很简单,但是我相信会有很大 ...

  4. 强制类型转换(int)、(int&)和(int*)的区别

    我们先来看两行代码: float x=1.75,y=1.75; cout<<(int)x<<" "<<(int&)y<<en ...

  5. 使用apache benchmark(ab) 测试报错: apr_socket_recv: Connection timed out (110)

    使用ab( apache benchmark )测试的时候,使用如下命令: ab -n 15000 -c 200   http://localhost/abc/abc.php 执行操作一定条数,或连续 ...

  6. 好久没写了,总结一下lnux常用的命令(基础)

    Linux 1.init 0 关机 2.init 6  重启 3.ls 列出当前目录下的文件 4.cd  切换目录  cd -  切换最近使用的两次目录 5.pwd 查看当前所在的路径 (“-”为用户 ...

  7. jenkins+maven+svn 自动化部署

    背景: 公司的web平台使用JAVA写的,但是不是用Tomcat部署的,代码内部自带了Web服务器,所以只需要有JAVA环境,将代码打包上传,启动脚本就可以. 项目是根据pom.xml打包成的是.zi ...

  8. Python基础——字典(dict)

    由键-值对构建的集合. 创建 dic1={} type(dic1) dic2=dict() type(dic2) 初始化 dic2={'hello':123,'world':456,'python': ...

  9. matplotlib学习记录 五

    # 绘制电影票房竖条形图 from matplotlib import pyplot as plt a = ["战狼2","速度与激情8","功夫瑜伽 ...

  10. matplotlib 设置图形大小时 figsize 与 dpi 的关系

    matplotlib 中设置图形大小的语句如下: fig = plt.figure(figsize=(a, b), dpi=dpi) 其中: figsize 设置图形的大小,a 为图形的宽, b 为图 ...