【LeetCode 337 & 329. memorization DFS】House Robber III
/**
* 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的更多相关文章
- 【LeetCode】House Robber III(337)
1. Description The thief has found himself a new place for his thievery again. There is only one ent ...
- 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...
- 【LeetCode Weekly Contest 26 Q3】Friend Circles
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...
- 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
- 【LeetCode Weekly Contest 26 Q1】Longest Uncommon Subsequence I
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
- 【leetcode边做边学】二分查找应用
很多其它请关注我的HEXO博客:http://jasonding1354.github.io/ 简书主页:http://www.jianshu.com/users/2bd9b48f6ea8/lates ...
- 1. 两数之和【Leetcode中国,by java】
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], target ...
- 【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 ...
- 【LeetCode 235_二叉搜索树】Lowest Common Ancestor of a Binary Search Tree
解法一:递归 TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if (root == NULL | ...
随机推荐
- 解决gradle多模块依赖在Idea中能运行,gradle build失败的问题。
最近需要初始化一个SpringBoot新项目遇到一个问题就是:项目中有多个子模块,使用gradle依赖管理成功. 项目结构如下: project --module1 --module2我的mo ...
- ios 短音效的使用
1.通用短音效ID的获取 #import <Foundation/Foundation.h> @interface MJAudioTool : NSObject /** * 播放音效 * ...
- spring cloud - config 属性自动刷新
启动config-server,启动成功后就不需要在管了; 在config-client做些修改: 在使用的controller或service的类上加上一个注解@RefreshScope 在pom中 ...
- (C/C++)register关键字
register:这个关键字的作用是请求编译器尽可能的将变量存在CPU内部寄存器中,而不是通过内存寻址访问,以提高效率. 注意是尽可能,不是绝对.一个CPU 的寄存器也就那么几个或几十个,你要是定义了 ...
- Android ADB实现解析【转】
本文转载自:http://blog.csdn.net/u010223349/article/details/41120255 ADB是Android系统提供的调试工具,整个ADB工具由三部分组成: ...
- POJ 2017 Speed Limit (直叙式的简单模拟 编程题目 动态属性很少,难度小)
Sp ...
- HashSe、LinkedHashSet、TreeSet(java基础知识十七)
1.HashSet存储字符串并遍历 * 特点:无序.无索引.无重复 HashSet存储字符串并遍历 HashSet<String> hs = new HashSet<>(); ...
- android之View坐标系(view获取自身坐标的方法和点击事件中坐标的获取)
在做一个view背景特效的时候被坐标的各个获取方法搞晕了,几篇抄来抄去的博客也没弄很清楚. 现在把整个总结一下. 其实只要把下面这张图看明白就没问题了. 涉及到的方法一共有下面几个: view获取自身 ...
- codeforces 669C C. Little Artem and Matrix(水题)
题目链接: C. Little Artem and Matrix time limit per test 2 seconds memory limit per test 256 megabytes i ...
- RxJava RxBinding 按钮(Button) 点击(click)
/********************************************************************* * RxJava RxBinding 按钮(Button) ...