【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 ...
随机推荐
- MySql常用语句总结更新
1.Mysql修改字段的默认值: alter table tablename alter column drop default; (若本身存在默认值,则先删除) alter table tablen ...
- 相同name,取最小的id的值,mysql根据相同字段 更新其它字段
id name info1 a 1232 a 2353 a 1244 b 125 b 987相同name,取最小的id的值id name info1 a 1232 a 1233 a 1234 b 12 ...
- day27—JavaScript实现定时器及其应用案例
转行学开发,代码100天——2018-04-12 JavaScript中定时器有两种,分别是setInterval和setTimeout;其用法如下: 开启: setTimeout("fun ...
- 神经网络 fann 教程 英文 以及 翻译 参考
http://fann.sourceforge.net/fann_en.pdf http://blog.csdn.net/fengshuiyue/article/details/41446257
- Python笔记(十三)_os模块和os.path模块
os模块中关于文件/目录常用的函数使用方法 getcwd() 返回当前工作目录 chdir(path) 改变工作目录 listdir(path='.') 列举指定目录中的文件名('.'表示当前目录,' ...
- 04 | 基础篇:经常说的 CPU 上下文切换是什么意思?(下)
上一节,我给你讲了 CPU 上下文切换的工作原理.简单回顾一下,CPU 上下文切换是保证 Linux 系统正常工作的一个核心功能,按照不同场景,可以分为进程上下文切换.线程上下文切换和中断上下文切换. ...
- Mac002--Git安装
Mac--Git安装 注意:在安装Git前,可先安装homebrew,应用brew命令安装Git即可. 一.Mac 安装homebrew 参考博客:https://blog.csdn.net/yuex ...
- mysql内存数据淘汰机制和大查询会不会把内存打爆?
首先我们说一下大查询会不会把内存打爆? 比如说主机内存有5g,但是我们一个大查询的数据有10g,这样会不会把内存打爆呢? 答案:不会 为什么? 因为mysql读取数据是采取边读边发的策略 select ...
- eclipse以及myeclipse的xml配置文件没有提示的问题解决
对于在使用hibernate时,需要对配置文件进行配置,我们需要引入dtd约束文件.在有网的情况下,可以直接从网上下载,编写xml配置文件的时候,可以提示:在没网的情况下,那么就提示不出来. 下面的方 ...
- Spring cloud学习--Zuul02
过滤器 Zuul包括两部分内容:请求的路由和过滤.而实际上请求的路由也是通过过滤器实现的,例如理由映射主要通过pre类型的过滤器完成,它将请求路径与配置的路由规则进行匹配,找到需要转发的目标地址:请求 ...