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. 关于 NetBackup 应答文件(/tmp/NBInstallAnswer.conf)

    关于 NetBackup 应答文件 在 UNIX 和 Linux 安装和升级期间使用 NetBackup 应答文件 (/tmp/NBInstallAnswer.conf),以便: 覆盖某些默认值. 避 ...

  2. 使用FontDialog组件设置字体

    实现效果: 知识运用: FontDialog组件的Font属性 //获取或设置选定的字体 public Font Font  { get;set; } 实现代码: private void butto ...

  3. Oracle Real Application Clusters (RAC)

    Oracle Real Application Clusters — 概述 包含 Oracle Real Application Clusters (RAC) 选件的 Oracle 数据库允许依托一组 ...

  4. JavaScript -- 条件语句和循环语句

    if语句 在我们开发程序的时候,经常会遇到选择题,例如,年龄大于18,你就可以抽烟喝酒烫头,年龄小于18,你就只能吃饭喝水.在我们的代码中,我们可以用if语句来实现这种判断 语法一: if( cond ...

  5. Dojo的declare接口

    declare(classname,[],{}) declare的第一个参数是可选的,代表类的名称 declare的第二个参数代表类的继承关系,比如继承哪一个父类,可以看到:第二个参数是一个数组,所以 ...

  6. Spring3中好用的工具类收集

    1) 请求工具类 org.springframework.web.bind.ServletRequestUtils //取请求参数的整数值: public static Integer getIntP ...

  7. 使用xcode 8 调试ios10

    这几天更新了ios10,发现真机不能调试,弹出几个错,表示没有证书.用ios9的真机能调试, 真他么坑,总结一下解决方法. 在BuildSetting 的Signing中Code Signing Id ...

  8. 201621123080《Java程序设计》第9周学习总结

    作业09-集合与泛型 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 2. 书面作业 本次作业题集集合 1. List中指定元素的删除(题集题目) 1.1 实 ...

  9. js替换函数用法

    定义和用法 replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. 语法 stringObject.replace(regexp/substr,replac ...

  10. MYSQL安装与库的基本操作

    mysql数据库 什么是数据库 # 用来存储数据的仓库 # 数据库可以在硬盘及内存中存储数据 数据库与文件存储数据区别 数据库本质也是通过文件来存储数据, 数据库的概念就是系统的管理存储数据的文件 数 ...