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. STM32 进行软件复位的方法

    platform:stm32f103xx include:core_cm3.h /** \brief System Reset \details Initiates a system reset re ...

  2. {bzoj2338 [HNOI2011]数矩形 && NBUT 1453 LeBlanc}平面内找最大矩形

    思路: 枚举3个点,计算第4个点并判断是否存在,复杂度为O(N3logN)或O(N3α) 考虑矩形的对角线,两条对角线可以构成一个矩形,它们的长度和中点必须完全一样,于是将所有线段按长度和中点排序,那 ...

  3. 豹子安全-注入工具-显错注入-asp_POST_显错_SQLServer_GetWebShell

    豹子安全-注入工具-显错注入-SQL Server-上传WebShell 请看下列视频 . 该视频在停留10秒钟后开始. 或点击此处查看:豹子安全-注入工具-asp_POST_显错_SQLServer ...

  4. python监听、操作键盘鼠标库pynput详细教程

    § 0.0.0 前言 监听.操作鼠标.键盘是实现自动化的捷径,比如我实现自动化签到用到了模拟键盘操作. pynput是监听.操控鼠标和键盘的跨平台第三方python库. 你可以通过pip insnal ...

  5. Vuser发生器

    一.脚本开发过程: 1.计划:收集测试信息,整理业务逻辑,制定测试计划 2.录制脚本: 新建脚本---选择脚本协议(单协议脚本:多协议脚本:最近使用过协议)选择协议---开始录制脚本 脚本录制时,Vu ...

  6. C:习题1

    C 语言具有哪些主要特点? 1.兼高级语言和汇编语言优点的语言 2.一种结构化程序设计语言 3.语言数据类型丰富 4.具有种类丰富的运算符 5.有预处理功能 C 语言的主要用途是什么? 1.作为一种系 ...

  7. 力扣题解-面试题22. 链表中倒数第K个节点

    题目描述 输入一个链表,输出该链表中倒数第k个节点.为了符合大多数人的习惯,本题从1开始计数,即链表的尾节点是倒数第1个节点.例如,一个链表有6个节点,从头节点开始,它们的值依次是1.2.3.4.5. ...

  8. linux常用命令---计划定时任务

    计划定时任务(crontab) 存放定时任务的文件 /var/spool/cron systemctl status cron ps -ef|grep crond 检测crontab是否开机启动 sy ...

  9. 使用包时,报 xxx.default is not a function

     最近做了一个导出功能,代码如下 import request from 'request-promise-native'; export default class Form { // 导出 @po ...

  10. HTML使用正则验证

    制作HTML前台用户验证等,需要对用户名或者密码进行验证,这时使用正则表达式能够精确地对text进行限制. 具体在HTML中的运用代码如下: 转自 https://blog.csdn.net/weix ...