【leetcode】823. Binary Trees With Factors
题目如下:
Given an array of unique integers, each integer is strictly greater than 1.
We make a binary tree using these integers and each number may be used for any number of times.
Each non-leaf node's value should be equal to the product of the values of it's children.
How many binary trees can we make? Return the answer modulo 10 ** 9 + 7.
Example 1:
Input:A = [2, 4]
Output: 3
Explanation: We can make these trees:[2], [4], [4, 2, 2]Example 2:
Input:A = [2, 4, 5, 10]
Output:7
Explanation: We can make these trees:[2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].Note:
1 <= A.length <= 1000.2 <= A[i] <= 10 ^ 9.
解题思路:假设i*j = k,记dp[k] 为 k作为树的根节点时可以构成的树的数量,那么有dp[k] = 1 + dp[i] * dp[j] (1 为树中只有一个根节点的情况),如果同时还有m*n = k,那么有dp[k] = 1 + dp[i] * dp[j] + dp[m]*dp[n],有了状态转移方程后,题目就很简单了。对A进行排序,这样可以保证k > i & k > j 如果A[k] = A[i] * A[j] 。接下来对A进行遍历,同时嵌套一层循环,如果满足A[i] * A[j] = A[k],就令dp[k] += dp[i] * dp[j]。最后的答案就是sum(dp)。
代码如下:
class Solution(object):
def numFactoredBinaryTrees2(self, A):
A.sort()
dic = {}
dp = [1] * len(A)
for i in range(len(A)):
dic[A[i]] = i
for j in range(i):
if A[i] % A[j] == 0 and A[i] / A[j] in dic:
v = A[i] / A[j]
dp[i] += dp[j] * dp[dic[v]]
return sum(dp) % (10**9 + 7)
【leetcode】823. Binary Trees With Factors的更多相关文章
- 【LeetCode】823. Binary Trees With Factors 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
- 【leetcode】 Unique Binary Search Trees (middle)☆
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【LeetCode】Validate Binary Search Tree ——合法二叉树
[题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...
- 【LeetCode】145. Binary Tree Postorder Traversal
Difficulty: Hard More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...
- 【LeetCode】二叉查找树 binary search tree(共14题)
链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...
- 【LeetCode】Balanced Binary Tree 解题报告
[题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...
- 【LeetCode】872. Leaf-Similar Trees 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 中序遍历 先序遍历 后序遍历 日期 题目地址:htt ...
- 【leetcode】Unique Binary Search Trees (#96)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
随机推荐
- ResquestInfoServlet类通过访问HttpServletRequest对象的各种方法来读取HTTP请求中的特定信息,并且把它们写入到HTML中
ResquestInfoServlet类通过访问HttpServletRequest对象的各种方法来读取HTTP请求中的特定信息,并且把它们写入到HTML中 ResquestInfoServlet.j ...
- HDU6668 Polynomial(模拟)
HDU6668 Polynomial 顺序遍历找出最高次幂项的系数 分三种情况 \(1/0\).\(0/1\).\(f(x)/g(x)\) . 复杂度为 \(O(n)\) . #include< ...
- jmeter 命令行运行与生成报告
一. 使用命令行方式运行Jmeter 1.1 为什么 使用GUI方式启动jmeter,运行线程较多的测试时,会造成内存和CPU的大量消耗,导致客户机卡死. 所以正确的打开方式是在GUI模式下调 ...
- Visual Studio Code - 快捷键
默认快捷键 Visual Studio Code 默认快捷键 代码提示(自动补全,自动完成) 默认是快捷键是Ctrl+Space,与搜狗输入法切换中英文的快捷键冲突了..可以改搜狗输入法的快捷键(Sh ...
- Django CORS跨域资源共享
1,什么是CORS 允许浏览器向跨源(协议 + 域名 + 端口)服务器发出XMLHttpRequest请求,从而克服了AJAX只能同源使用的限制 2,特点 1,浏览器自动完成(在请求头中加入特 ...
- MyBatis中的$和#,用不好,准备走人!
作者:程序猿的内心独白 https://m.toutiaocdn.com/i6685496024770806280 这是一次代码优化过程中发现的问题,在功能优化后发现部分数据查不到出来了,问题就在于一 ...
- 【转】Linux下vim的基本操作
原文链接 Linux vi/vim 所有的 Unix Like 系统都会内建 vi 文书编辑器,其他的文书编辑器则不一定会存在. 但是目前我们使用比较多的是 vim 编辑器. vim 具有程序编辑的能 ...
- 攻防世界--csaw2013reversing2
测试文件:https://adworld.xctf.org.cn/media/task/attachments/3f35642056324371b913687e770e97e6.exe 1.准备 打开 ...
- R语言子集
取子集方法 x[]:适用于所有r语言 x[[ ]]:适用于list或者data.frame中提取元素 x$:使用元素名做索引,提取list或者data.frame中的某个元素 注意,取出的子集数据类型 ...
- rk3288 android5.1 修改时区
/work/rk3288/firefly-rk3288_android5.1_git_20180126/device/rockchip/rk3288/rk3288_box/system.prop 修改 ...