Java实现 LeetCode 823 带因子的二叉树(DP)
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)的更多相关文章
- [LeetCode] Binary Trees With Factors 带因子的二叉树
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree us ...
- [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 ...
- Java实现 LeetCode 837 新21点(DP)
837. 新21点 爱丽丝参与一个大致基于纸牌游戏 "21点" 规则的游戏,描述如下: 爱丽丝以 0 分开始,并在她的得分少于 K 分时抽取数字. 抽取时,她从 [1, W] 的范 ...
- 【Java】 大话数据结构(9) 树(二叉树、线索二叉树)
本文根据<大话数据结构>一书,对Java版的二叉树.线索二叉树进行了一定程度的实现. 另: 二叉排序树(二叉搜索树) 平衡二叉树(AVL树) 二叉树的性质 性质1:二叉树第i层上的结点数目 ...
- 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 ...
- 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. ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- jquery判断邮箱及密码是否输入正确的表单提交
jquery我接触的也不是很多,基本就是照着案例然后查相关方法做出来的,基本用了大概半天的时间,手打加查资料实现.具体如下,首先下载一个jquery包,网址是https://jquery.com/do ...
- uCOS2014.1.7
主要关于任务堆栈: 在计算机中一般设置一个专用的地址寄存器用来存放堆栈的栈顶地址,这个寄存器称为堆栈指针(SP). 任务堆栈有两种,一种是地址向下增长的,PC就是采用这样的堆栈: 另一种是地址向上增长 ...
- [hdu5195]线段树
题意:给一个DAG,最多可以删去k条边,求字典序最大的拓扑序列.思路:贪心选取当前可选的最大编号即可,同时用线段树维护下.一个节点可以被选,当且仅当没有指向它的边. #include <iost ...
- vue 下拉刷新实现
[手动实现下拉刷新]可以用vue-pull-refash 插件代替 //下拉刷新 let scroll = this.$ref.scroll // 获取当前要拖拽的元素 let top = scrol ...
- flink入门学习
Flink学习笔记 一.简介 1.定义: 针对流数据和批数据的分布式处理引擎.它主要是由 Java 代码实现.. 2.应用场景: 流数据:把所有任务当成流来处理,处理观察和分析连续事件产生的数 ...
- SpringBoot 整合Mail发送功能问题与解决
SpringBootLean 是对springboot学习与研究项目,是根据实际项目的形式对进行配置与处理,欢迎star与fork. [oschina 地址] http://git.oschina.n ...
- Docker学习笔记(二):端口映射与容器互联
端口映射 使用docker run时,可以指定-P(大写)与-p(小写)参数映射端口. docker run -P -P(大写)会随机映射一个端口到容器的内部端口 -> [feifei@ffma ...
- Spring Boot 之 Spring Batch 批处理实践
实践内容 从 MariaDB 一张表内读 10 万条记录,经处理后写到 MongoDB . 具体实现 1.新建 Spring Boot 应用,依赖如下: <!-- Web 应用 --> & ...
- xml rpc SimpleXMLRPCServer [python]
SimpleXMLRPCServe 其实里面xml的概念不是很强,主要是rpc !不用关心什么xml . rpc 是就是远程调用,把函数什么的放到远程服务器上,本地调用就行了.用 SimpleXMLR ...
- Vue element-ui date-picker 结束时间大于开始时间且开始时间小于此刻(转)
转载自:https://blog.csdn.net/wintersweetGirl/article/details/82461412 <el-form ref="form" ...