823. 带因子的二叉树

给出一个含有不重复整数元素的数组,每个整数均大于 1。

我们用这些整数来构建二叉树,每个整数可以使用任意次数。

其中:每个非叶结点的值应等于它的两个子结点的值的乘积。

满足条件的二叉树一共有多少个?返回的结果应模除 10 ** 9 + 7。

示例 1:

输入: A = [2, 4]

输出: 3

解释: 我们可以得到这些二叉树: [2], [4], [4, 2, 2]

示例 2:

输入: A = [2, 4, 5, 10]

输出: 7

解释: 我们可以得到这些二叉树: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].

提示:

1 <= A.length <= 1000.

2 <= A[i] <= 10 ^ 9.

PS:

直接找能%的,并且余数为0,

从小到大找,有剪枝操作

class Solution {
public int numFactoredBinaryTrees(int[] A) {
int size = A.length;
Arrays.sort(A);
long[] dp = new long[size];
long ans = 1;
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < size; i++) map.put(A[i], i); dp[0] = 1;
for (int i = 1; i < size; i++) {
int vi = A[i];
long curres = 1;
for (int j = 0; j < i; j++) {
int vj = A[j];
if (vj * vj > vi) break;
Integer nj;
if (vi % vj == 0 && (nj = map.get(vi/vj)) != null) {
curres += dp[j] * dp[nj] * (nj == j ? 1 : 2);
curres %= 1000000007;
}
}
ans += (dp[i] = curres);
}
return (int)(ans % 1000000007);
} }

Java实现 LeetCode 823 带因子的二叉树(DP)的更多相关文章

  1. [LeetCode] Binary Trees With Factors 带因子的二叉树

    Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree us ...

  2. [Swift]LeetCode823. 带因子的二叉树 | Binary Trees With Factors

    Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree us ...

  3. Java实现 LeetCode 837 新21点(DP)

    837. 新21点 爱丽丝参与一个大致基于纸牌游戏 "21点" 规则的游戏,描述如下: 爱丽丝以 0 分开始,并在她的得分少于 K 分时抽取数字. 抽取时,她从 [1, W] 的范 ...

  4. 【Java】 大话数据结构(9) 树(二叉树、线索二叉树)

    本文根据<大话数据结构>一书,对Java版的二叉树.线索二叉树进行了一定程度的实现. 另: 二叉排序树(二叉搜索树) 平衡二叉树(AVL树) 二叉树的性质 性质1:二叉树第i层上的结点数目 ...

  5. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  6. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  7. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  8. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  9. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

随机推荐

  1. search(10)- elastic4s-multi_match:多字段全文搜索

    在全文搜索中我们常常会在多个字段中匹配同一个查询条件或者在不同的字段中匹配不同的条件.比如下面这个例子: GET /books/_search { "query": { " ...

  2. Day_14【IO流】扩展案例2_缓冲字符输出、输入流进行用户名的创建

    需求分析 1.项目根目录下建立文件: user.txt,文件中存放用户名和登录密码,格式:用户名,密码,如:aaa,123: 2.user.txt文件中初始存放的用户信息有如下: jack,123 r ...

  3. 初探numpy

    安装numpy 通过python pip安装numpy pip install numpy numpy ndarray对象 创建ndarray对象只需调用numpy的array函数即可 numpy.a ...

  4. 【5min+】美化API,包装AspNetCore的返回结果

    系列介绍 [五分钟的dotnet]是一个利用您的碎片化时间来学习和丰富.net知识的博文系列.它所包含了.net体系中可能会涉及到的方方面面,比如C#的小细节,AspnetCore,微服务中的.net ...

  5. Python脚本:实现对象集合List导入到excel表格,支持mysql,postergrsql,MongoDB

    import xlwt import os import datetime #验证export_filed中的字段是否在对象字段中 def checkField(obj_list,filed_dict ...

  6. 切片原型[start:stop:step]

    切片操作符在Python中的原型是 [start:stop:step] 步长值:默认是一个接着一个切取,如果为2,则表示进行隔一取一操作.步长值为正时表示从左向右取,如果为负,则表示从右向左取.步长值 ...

  7. STM32编程:是时候深入理解栈了

    [导读] 从这篇文章开始,将会不定期更新关于嵌入式C语言编程相关的个人认为比较重要的知识点,或者踩过的坑. 为什么要深入理解栈?做C语言开发如果栈设置不合理或者使用不对,栈就会溢出,溢出就会遇到无法预 ...

  8. Linux常用命令~新手必知

    ifconfig查看本机IP ls  查看当前目录下所有文件及文件夹 ll      以详细方式显示所有文件与文件夹.(相当于 ls -l 命令) pwd     显示当前目录路径 cd  /user ...

  9. Docker之save、load、export、import命令

    Docker的镜像和容器可以有两种方式导出 1.Docker save #ID or #Name 2.Docker save #ID or #Name docker save和docker expor ...

  10. Python之sys.arg[]的用法解释

    转载自:https://www.cnblogs.com/liangmingshen/p/8906148.html sys.argv[]说白了就是一个从程序外部获取参数的桥梁,这个“外部”很关键,所以那 ...