当前位置:网站首页>leetcode 823. Binary Trees With Factors(因子二叉树)
leetcode 823. Binary Trees With Factors(因子二叉树)
2022-08-10 11:00:00 【蓝羽飞鸟】
Given an array of unique integers, arr, where each integer arr[i] 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 its children.
Return the number of binary trees we can make. The answer may be too large so return the answer modulo 109 + 7.
Example 1:
Input: arr = [2,4]
Output: 3
Explanation: We can make these trees: [2], [4], [4, 2, 2]
Example 2:
Input: arr = [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].
Constraints:
1 <= arr.length <= 1000
2 <= arr[i] <= 109
All the values of arr are unique.
数组里面的数字可用来作树的节点,可以重复使用。
非叶子的节点的数字要等于它的children节点的乘积。
节点可以单独组成二叉树。
问可以组成多少个这样的二叉树。
思路:
首先想到的是要从小到大组成树,这样大的数字才能是小的数字的乘积,所以要把数组排序。
然后可以想到的是root的个数应该是左子树个数与右子树个数的乘积,
比如左子树有5种,右子树有6种,
那么左边的每一种都可以对应右边的6种,所以是乘法。
那么每次是不是都需要计算左右子树的个数?如果都计算,遇到重复的数字就会发生重复的计算,
所以要用DP,保存已经计算好的。
用dp[i]表示数字 可以形成的二叉树个数。
每个数字可单独形成二叉树,所以dp[i] = 1。
arr[i]不是连续的数字,所以用hashMap作为dp数组,
从小到大遍历数字,遇到 是2个数乘积的,dp[i] += dp[左] * dp[右], 其中左 右 =
最后把hashMap中的value相加。
注意相加时可能超出int的最大范围,所以用long, 不要忘了 % mod。
public int numFactoredBinaryTrees(int[] arr) {
int n = arr.length;
int mod = 1000000007;
Long res = 0l;
HashMap<Integer, Long> dp = new HashMap<>();
Arrays.sort(arr);
for(int i = 0; i < n; i ++) {
//arr下标
dp.put(arr[i], 1l);
for(int j = 0; j < i; j ++) {
//arr下标
if(arr[i] % arr[j] == 0 && dp.containsKey(arr[i] / arr[j])) {
dp.put(arr[i], (dp.get(arr[i]) + dp.get(arr[j]) * dp.get(arr[i]/arr[j]))%mod);
}
}
}
for(Long values : dp.values()) {
res += values;
}
return (int)(res % mod);
}
边栏推荐
- Do self-media monthly income tens of thousands?Several self-media tools that bloggers are using
- 【机器学习】浅谈正规方程法&梯度下降
- Interviewer: How are Dao, Service, Controller, Util, and Model divided in the project?
- 内存问题难定位,那是因为你没用ASAN
- 快手“弃”有赞与微盟“结亲”,电商SaaS行业竞争格局将变?
- Hangdian Multi-School-Loop-(uncertainty greedy + line segment tree)
- GPU accelerated Pinterest recommendation model, the number of parameters increased by 100 times, and the user activity increased by 16%
- [Brave food, not afraid to write the linked list] The problem of the penultimate node of the linked list
- AUTOCAD - reducing spline curve control points, the advanced CAD practice (3)
- 英特尔推送20220809 CPU微码更新 修补Intel-SA-00657安全漏洞
猜你喜欢

rider内Mono脚本找不到引用资源

金九银十跳槽旺季:阿里、百度、京东、美团等技术面试题及答案

runtime-core.esm-bundler.js?d2dd:218 Uncaught TypeError: formRef.value?.validate is not a function

GPU加速Pinterest推荐模型,参数量增加100倍,用户活跃度提高16%

OneFlow source code parsing: operator instructions executed in a virtual machine

内存问题难定位,那是因为你没用ASAN

mysql出现:ERROR 1524 (HY000): Plugin ‘123‘ is not loaded

Some tips for using Unsafe

VSCode远程连接服务器报错:Could not establish connection to “xxxxxx”的可能错误原因及解决

推荐6个自媒体领域,轻松易上手
随机推荐
POJ 2891 Strange Way to Express Integers (扩展欧几里得)
2022年裁员潮,失业程序员何去何从?
第3章-线性方程组(3)
AUTOCAD - reducing spline curve control points, the advanced CAD practice (3)
Pycharm终端出现PS问题、conda或activate不是内部命令问题..
【勇敢饭饭,不怕刷题之链表】链表反转的几种情况
CPU多级缓存与缓存一致性
Emulate stm32 directly with proteus - the programmer can be completely discarded
Weilai-software development engineer side record
3款不同类型的自媒体免费工具,有效提高创作、运营效率
中小规模网站架构
力扣练习——59 从二叉搜索树到更大和树
使用JMeter进行MySQL的压力测试
2023版揽胜运动曝光,安全、舒适一个不落
[Brave food, not afraid of the linked list of brushing questions] Merging of ordered linked lists
Research on motion capture system for indoor combined positioning technology
突破次元壁垒,让身边的玩偶手办在屏幕上动起来!
From the product dimension, why can't we fully trust Layer2?
电脑怎么设置屏幕息屏时间(日常使用分享)
[Brave food, not afraid to write the linked list] The problem of the penultimate node of the linked list