【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 ...
随机推荐
- 170817关于AJAX的知识点
1.AJAX [1] AJAX简介 全称: Asynchronous JavaScript And XML ...
- StringOfChar 将一个字符重复多次 形成一个 字符串
StringOfChar Returns a string with a specified number of repeating characters. In Delphi code, Strin ...
- JSP表单提交 与 接受显示
Demo01.jsp 提交表单输入的信息至 Demo02.jsp方法一 1 <%@ page language="java" contentType="text/h ...
- jQuery 动态添加、删除css样式
1.addClass css中: <style type="text/css"> .chColor {background: #267cb7;color:w ...
- JavaScript List
function List() { this.listSize = 0; this.pos = 0; this.dataSource = []; this.clear = fu ...
- svg画圆环
之前我已经分享了一篇css画圆环,为啥今天还要分享一篇svg画圆环呢? 原因是:css画圆环在部分ipone手机会有bug,最大张角为90°,所以圆环会有白色的间隙. 好了,开始代码展示: html: ...
- 应该用forEach改变数组的值吗? 原生JS forEach()和map()遍历的异同点
应该用forEach改变数组的值吗? https://segmentfault.com/q/1010000013170900?utm_source=index-hottest 由于js中的数组是引用类 ...
- An easy problem (位运算)
[题目描述] 给出一个整数,输出比其大的第一个数,要求输出的数二进制表示和原数二进制表示下1的个数相同. [题目链接] http://noi.openjudge.cn/ch0406/1455/ [算法 ...
- What is one-hot?
SEO: libtorch 如何 OneHot ? torch OneHot 源代码 ? https://www.tensorflow.org/api_docs/python/tf/one_hot 最 ...
- jquery的扩展,及编辑插件的书写格式
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...