【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 ...
随机推荐
- 容器宽高不确定,图片宽高不确定,css如何实现图片响应式?
图片响应式 在响应式开发中最烦恼的应该就是图片了,虽然图片设置max-width: 100%;可以让图片宽度占满容器,但是高度就不能自适应了.如果将容器高度限死,那么我们就要使用媒体查询来控制容器的高 ...
- HDU4336 Card Collector (概率dp+状压dp)
http://acm.hdu.edu.cn/showproblem.php?pid=4336 题意:有n种卡片,一个包里会包含至多一张卡片,第i种卡片在某个包中出现的次数为pi,问将所有种类的卡片集齐 ...
- PHP垃圾回收深入理解
转摘于http://www.cnblogs.com/lovehappying/p/3679356.html PHP是一门托管型语言,在PHP编程中程序员不需要手工处理内存资源的分配与释放(使用C编写P ...
- centos mysql初探 -- 配置、基本操作及问题
目录: centos安装mysql 使用mysql客户端进行简单操作 python2和python3连接mysql mysql导入文件问题 死锁解决办法 windows 7 远程连接 mysql 服务 ...
- 133、TensorFlow加载模型(二)
# 选择哪个变量来保存和恢复 # 如果你没有传递任何的参数到tf.train.Saver() # 这个saver会处理计算图中所有的变量 # 每一个变量都被保存,保存的名字就是当初创建他们时候的名字 ...
- day12—jQuery ui引入及初体验
转行学开发,代码100天——2018-03-28 按照所下载教学视频,今天已进行到jQuery UI的学习中.注:本人所用教学视频不是太完整,介绍的内容相对简单,有些只是带过.其他时间中,仍需继续针对 ...
- 【GDAL】聊聊GDAL的数据模型(二)——Band对象
在GDAL中栅格数据直接参与各种计算的重要对象是Band 摘录官方描述: Raster Band A raster band is represented in GDAL with the GDALR ...
- Mybatis入门(附源码压缩包下载)
首先,来个项目全景预览,文章尾部附上Demo下载链接 [1]pom.xml配置(加入jar包) <project xmlns="http://maven.apache.org/POM/ ...
- Git001--简介
Git--简介 本文来自于:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00 ...
- from、includes、indexOf
from.includes.indexOf:https://blog.csdn.net/j59580/article/details/53897630?utm_source=blogxgwz1 语法 ...