/**
* 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. HTTP请求中带有特殊字符"|",返回400错误

    Java平台,服务器是Tomcat8,前端ajax访问服务器时,F12返回400错误,经分析,URL地址中get传参值里面含有“|“, Invalid character found and RFC ...

  2. Mac OS用minikube安装单节点kubernetes

    参考 https://kubernetes.io/docs/tasks/tools/install-minikube/ https://github.com/linianhui/code/blob/m ...

  3. 使用cwRsync在Windows的目录之间增量同步文件

    http://www.qiansw.com/using-cwrsync-in-the-windows-directory-between-the-incremental-synchronization ...

  4. id、NSObject *、id<NSObject>、instancetype

    1. id 与 NSObject * (1) id 是 Objective-C 对象,但是并不一定是NSObject对象,并非所有的Foundation/Cocoa对象都是继承于NSObject对象的 ...

  5. FZU1977 Pandora adventure —— 插头DP

    题目链接:https://vjudge.net/problem/FZU-1977  Problem 1977 Pandora adventure Accept: 597    Submit: 2199 ...

  6. linux初级学习笔记三:linux操作系统及常用命令,及如何复制和移动文件!(视频序号:02_4)

    本节学习的命令:cp,mv,install,du,read 本节学习的技能:文件的移动与复制 cp( copy):复制和移动文件 cp SRC DEST -r:递归复制一个目录及其目录中的所有文件 - ...

  7. BZOJ_1563_[NOI2009]诗人小G_决策单调性

    BZOJ_1563_[NOI2009]诗人小G_决策单调性 Description Input Output 对于每组数据,若最小的不协调度不超过1018,则第一行一个数表示不协调度若最小的不协调度超 ...

  8. vue项目中的路径别名

    每次写引入组件的路径,如果路径嵌套比较深,那么会比较麻烦,我们可以在webpack.base.conf.js,中设置路径的别名,默认webpack设置src的别名为@ 建议配置src下一级目录的别名, ...

  9. Java:String之间通过==比较的情况

    大家都知道在String之间的内容比较的时候,是通过equals函数比较的. 但是在在许多的面试题中,总是出现一堆的判断两个String对象通过==比较的结果,实际上是考的Java内存分配机制. Ja ...

  10. UVaLive 6588 && Gym 100299I (贪心+构造)

    题意:给定一个序列,让你经过不超过9的6次方次操作,变成一个有序的,操作只有在一个连续区间,交换前一半和后一半. 析:这是一个构造题,我们可以对第 i 个位置找 i 在哪,假设 i  在pos 位置, ...