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. anaconda 安装各种库

    在anaconda prompt 添加清华源 https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/ conda config --add channe ...

  2. Ubuntu 忘记root user密码 关闭图形界面

    忘记root密码 删除recovery nomodeset 才删除的后面添加 quiet splash rw init=/bin/bash.然后按F10, 启动 忘记User密码 http://blo ...

  3. python整体图

  4. iOS JS 交互之利用系统JSContext实现 JS调用oc方法

    ios js 交互分为两块: 1.oc调用js 这一块实现起来比较简单, 我的项目中加载的是本地的html,js,css,需要注意的是当你向工程中拖入这些文件时,选择如下操作,(拖入的文件夹是蓝色的, ...

  5. 【转】EM算法原理

    EM是我一直想深入学习的算法之一,第一次听说是在NLP课中的HMM那一节,为了解决HMM的参数估计问题,使用了EM算法.在之后的MT中的词对齐中也用到了.在Mitchell的书中也提到EM可以用于贝叶 ...

  6. 洛谷P2347 砝码称重

    题目 貌似是某年提高组签到题,六重循环零压力AC,差点怒踩std 但本蒟蒻决定写正解——多重背包,果断20分 原因是写错了状态转移方程...神才知道我咋过的样例和两个测试点 扯远了 多重背包 简单说一 ...

  7. 【离线 撤销并查集 线段树分治】bzoj1018: [SHOI2008]堵塞的交通traffic

    本题可化成更一般的问题:离线动态图询问连通性 当然可以利用它的特殊性质,采用在线线段树维护一些标记的方法 Description 有一天,由于某种穿越现象作用,你来到了传说中的小人国.小人国的布局非常 ...

  8. 【dp】饥饿的牛

    普通dp题 题目描述 牛在饲料槽前排好了队.饲料槽依次用1到n(1 ≤ n ≤ 2000)编号.每天晚上,一头幸运的牛根据约翰的规则,吃其中一些槽里的饲料. 约翰提供b个区间的清单.一个区间是一对整数 ...

  9. mysql 添加数据如果数据存在就更新ON DUPLICATE KEY UPDATE和REPLACE INTO

    #下面建立game表,设置name值为唯一索引. CREATE TABLE `game` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar( ...

  10. C#基础-字符串

    字符串比较,strA.CompareTo(strB) A大于B 正数 A小于B 负数 A等于B 0 string strA = "ab"; string strB = " ...