/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
//递归程序就是结构演绎,有点像dp,只要定义好每一次递归过程完成的是同一个目标,就能保证所有递归结束之后完成的效果是最终的目标
int rob_naive(TreeNode* root) {// 返回当前节点开始的能够rob的最大值。
if(root == NULL) return ;//当前节点为空 0
int val = ;
if(root -> left)//左孩子不空
val += rob(root -> left -> left) + rob(root -> left -> right); //rob当前root 和 root 的孩子的孩子的能rob到的最大值。这里并不是rob孩子的孩子,而是孩子的孩子能rob的最大值就像这个递归结构每一次完成的目标一样。
if(root -> right)
val += rob(root -> right -> left) + rob(root -> right -> right);
return max(val + root -> val, rob(root -> left) + rob(root -> right));
//返回 rob(root, root's children 's children or root's children)
}
//看上面的方案(1330 ms):每一次我们都考虑了 root.left.left & root.left.right & root.right.left & root.right.right
// root.left & root.right 这里在递归计算的时候还是会算到上面计算过的值。
//所以给出一个考虑一步之后的优化记忆搜索。
int rob(TreeNode* root){
unordered_map<TreeNode*, int>mp;
return robMemeryDFS(root, mp);
}
//考虑一步的记忆化搜索(16 ms): 快了接近100倍
int robMemeryDFS(TreeNode* root, unordered_map<TreeNode*, int>&mp){
if(root == NULL) return ;
if(mp[root]) return mp[root];
int val = ;
if(root -> left)//左孩子不空
val += robMemeryDFS(root -> left -> left, mp) + robMemeryDFS(root -> left -> right, mp);
if(root -> right)
val += robMemeryDFS(root -> right -> left, mp) + robMemeryDFS(root -> right -> right, mp);
mp[root] = max(val + root -> val, robMemeryDFS(root -> left, mp) + robMemeryDFS(root -> right, mp));
return mp[root];
}
};

附加一道 同样使用记忆化搜索的题目 329. Longest Increasing Path in a Matrix

class Solution {
public:
int dir[][] = {{, }, {-, }, {, }, {, -}}, n, m;
int dfs(int x, int y, vector<vector<int>>& matrix, vector<vector<int>>&steps){
if(steps[x][y]) return steps[x][y];
int maxSteps = ; // record the longest path steps from (x, y)
for(int i = ; i < ; i ++){
int nx = dir[i][] + x, ny = dir[i][] + y, tmp = ;
if(nx >= && ny >= && nx < n && ny < m && matrix[nx][ny] > matrix[x][y]){
maxSteps = max(maxSteps, dfs(nx, ny, matrix, steps));
}
}
steps[x][y] = maxSteps + ; // + 1 for cur(x, y)
return steps[x][y];
}
int longestIncreasingPath(vector<vector<int>>& matrix) {
if(matrix.size() == ) return ;
n = matrix.size(), m = matrix[].size();
int ans = ;
vector<vector<int>>steps(n, vector<int>(m, ));
for(int i = ; i < n; i ++)
for(int j = ; j < m; j ++)
ans = max(ans, dfs(i, j, matrix, steps));
return ans;
}
};

【LeetCode 337 & 329. memorization DFS】House Robber III的更多相关文章

  1. 【LeetCode】House Robber III(337)

    1. Description The thief has found himself a new place for his thievery again. There is only one ent ...

  2. 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...

  3. 【LeetCode Weekly Contest 26 Q3】Friend Circles

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...

  4. 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...

  5. 【LeetCode Weekly Contest 26 Q1】Longest Uncommon Subsequence I

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...

  6. 【leetcode边做边学】二分查找应用

    很多其它请关注我的HEXO博客:http://jasonding1354.github.io/ 简书主页:http://www.jianshu.com/users/2bd9b48f6ea8/lates ...

  7. 1. 两数之和【Leetcode中国,by java】

    给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], target ...

  8. 【LeetCode刷题Java版】Reverse Words in a String

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  9. 【LeetCode 235_二叉搜索树】Lowest Common Ancestor of a Binary Search Tree

    解法一:递归 TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if (root == NULL | ...

随机推荐

  1. python 2: 解决python中的plot函数的图例legend不能显示中文问题

     问题: 图像标题.横纵坐标轴的标签都能显示中文名字,但是图例就是不能显示中文,怎么解决呢?  解决: plt.figure() plt.title(u'训练性能', fontproperties=f ...

  2. ICMP协议 广播以查询局域网内的所有主机

    看到了很多局域网内的主机扫描工具,在想怎么去实现这样一个工具.前几天看了Ping源码--ICMP协议的实例,ICMP可以用来探测网联网内的任一主机,ICMP和广播地址结合来扫描局域网内的所有主机不是很 ...

  3. JAVA变量存储

    1.java变量存储域 java变量的存储区域主要放在以下几个地方: (1)寄存器:可以说是最快的存储区,在C/C++中可以声明寄存器变量,但是在java中不能声明寄存器变量,只是编译器在编译时确定. ...

  4. navcat for mysql 连接远程数据库 教程

    1.首先进入数据库: mysql -uroot -p 2.然后打开数据库设置远程连接权限: mysql>GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%'ID ...

  5. Hive 特性及原理

    特点:Hive是构建在hadoop之上的数据仓库.数据存储在hdfs上,数据计算用的mapreduce框架.用户无需掌握MR的编写,通过类SQL语句即可自动生成查询计划. 主要内容:     接入入口 ...

  6. Bootloader与Kernel间参数传递机制 taglist【转】

    本文转载自:http://blog.csdn.net/tommy_wxie/article/details/9187821 Tag list被用来在bootloader和Linux kernel 之间 ...

  7. Android中android:visibility的3中属性的剖析

    在Android中控件或者布局的可见性android:visibility有3中情况,他们分别是: View.VISIBLE,View.UNVISIBLE,View.GONE View.VISIBLE ...

  8. [Selenium] 使用Chrome Driver 的示例

    //导入Selenium 库和 ChromeDriver 库 pachage com.learningselenium.simplewebdriver; import java.util.concur ...

  9. 【转】java对象——new对象的理解

    学了好长时间的java对于java中的对象一直没有理清楚,今天楼主对java中的对象进行了整理,希望对大家有帮助. 理解和使用java中的对象,我们首先了解一下构造方法与对象的创建.  类是面向对象语 ...

  10. ol 与ul 的区别

    1 <!DOCTYPE html> <html> <body> <ul> <li>咖啡</li> <li>牛奶< ...