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. 解密C语言编译背后的过程

    我们大部分程序员可能都是从C语言学起的,写过几万行.几十万行.甚至上百万行的代码,但是大家是否都清楚C语言编译的完整过程呢,如果不清楚的话,我今天就带着大家一起来做个解密吧. C语言相对于汇编语言是一 ...

  2. [hdu2119]二分图最小覆盖,最大匹配

    题意:给一个01矩阵,每次可以选一行或一列,打掉上面所有的1,求打掉所有的1所需的最小次数. 思路:经典的模型了,二分图最小覆盖=最大匹配.所谓最小覆盖是指选最少的点关联所有的边.容易得到将行和列看成 ...

  3. C# 数据操作系列 - 5. EF Core 入门

    0.前言 上一章简单介绍了一下ORM框架,并手写了一个类似ORM的工具类.这一章将介绍一个在C#世界里大名鼎鼎的ORM框架--Entity Framework的Core版. Entity Framew ...

  4. kubernetes部署redis主从高可用集群

    1.redis主从高可用集群结构 2.k8s部署有状态的服务选择 对于K8S集群有状态的服务,我们可以选择deployment和statefulset statefulset service& ...

  5. 盲注fuzz

    \'"%df'%df"and%201=1and%201=2'%20and%20'1'='1'%20and%20'1'='2"%20and%20"1"= ...

  6. lvm 日常操作。

    步骤: pvcreate /dev/sdb pvdisplay vgcreate volgroup /dev/sdb vgdisplay lvcreate -L2G volgroup lvdispla ...

  7. Django操作cookie实例

     cookie项目文件: templates模板: login.html {% load static %} <!DOCTYPE html> <html lang="en& ...

  8. tableView的嵌套

    1,subTableView需要开启多手势识别,多层tableView都会响应滚动事件(如果底层是scroll 依然会响应,这样滚动tableview时,scroll也会滚动,导致滚动过于灵活)2,通 ...

  9. Gym101630A Archery Tournament

    题目链接:https://vjudge.net/problem/Gym-101630A 题目大意: 有\(n\)个操作,每次输入\(t\) \(x\) \(y\)\((t=1,2; -10^9 \le ...

  10. SimpleAuthenticationInfo的参数

    SimpleAuthenticationInfo的参数 仅供个人参考,以及学习记录.SimpleAuthenticationInfo authenticationInfo = new SimpleAu ...