![](https://img-blog.csdnimg.cn/20201014180756923.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Leetcode动态规划
dtwd886
这个作者很懒,什么都没留下…
展开
-
leetcode337——打家劫舍III
方法一:树上dp,通过两个unordered_map记录每个点选择获取或者不选择获取的状态。选择则状态转移方程为f[o]=o->val+g[o->left]+g[o->right]不选择点的状态为g[o]=max(f[o->left],g[o->left])+max(f[o->right],g[o->right])/** * Definition for a binary tree node. * struct TreeNode { *原创 2020-08-05 22:08:59 · 93 阅读 · 0 评论 -
Leetcode120——三角形的最小路径和
题目链接:https://leetcode-cn.com/problems/triangle/思路:动态规划,直接左对齐,当j==0时dp[i][j]=dp[i-1][j]+c[i][j]当j==i时dp[i][j]=dp[i-1][j-1]+c[i][j]其余情况dp[i][j]=min(dp[i-1][j],dp[i-1][j-1])+c[i][j]注意,这个三角形数字数目是每层数目加1,开始误以为三角形不规则。导致DFS做法超时还可以考虑将j反向遍历,将空间复杂度优化为O(原创 2020-07-14 19:33:11 · 127 阅读 · 0 评论 -
Leetcode面试题17.13
题目链接:https://leetcode-cn.com/problems/re-space-lcci/submissions/思路:字典树实现倒序查找+dpclass Solution {struct Tree{ bool flag; Tree* child[26]={nullptr}; Tree() { flag=false; }};public: int respace(vector<string>&原创 2020-07-10 17:08:48 · 128 阅读 · 0 评论