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 ...
随机推荐
- Vue2.0 + ElementUI 手写权限管理系统后台模板(一)——简述
挤一下: 一开始以为没有多少人用就没建群,但是加我的人太多了,好多问题都是重复的,所以建个群大家互相沟通交流方便点,但是建的有点晚,错过了好多人所以群里人有点少,QQ群: 157216616 小提示 ...
- CPU瞒着内存竟干出这种事
还记得我吗,我是阿Q,CPU一号车间的那个阿Q. 今天忙里偷闲,来到厂里地址翻译部门转转,负责这项工作的小黑正忙得满头大汗. 看到我的到来,小黑指着旁边的座椅示意让我坐下. 坐了好一会儿,小黑才从工位 ...
- Spring整合Durid数据源
Spring整合Durid数据源 一.导入依赖 <!-- Druid数据源 --> <dependency> <groupId>com.alibaba</gr ...
- Analysis分析器
一.Analysis简介 场景执行过程中,loadrunner收集执行过程中的数据,存储在扩展名为.lrr的文件中,Analysis分析器打开这个文件,对文件信息进行处理,并生成图和报告. 数据分析不 ...
- Spring处理@Configuration的分析
Spring处理@Configuration的分析 声明:本文若有任何纰漏.错误,还请不吝指出! 序言 @Configuration注解在SpringBoot中作用很大,且不说SpringBoot中的 ...
- HTML5新特性 websocket(重点)--多对多聊天室
一.html5新特性 websocket(重点)--多对多聊天室 HTTP:超文本传输协议 HTTP作用:传输网页中资源(html;css;js;image;video;..) HTTP是浏览器搬运 ...
- 10.1Go Mysql
第十章 Go Mysql 准备好mysql数据库服务端数据. 创建test数据库 MariaDB [(none)]> create database test; Query OK, 1 row ...
- Mac Chrome浏览器取消自动升级(最新版)
做自动化突然冒出错误:SessionNotCreatedException: session not created: This version of ChromeDriver only suppor ...
- Kafka SSL安装与配置
1.概述 最近有同学咨询说,Kafka的SSL安全认证如何安装与使用?今天笔者将通过以下几个方面来介绍Kafka的SSL: Kafka 权限介绍 Kafka SSL的安装与使用 Kafka Eagle ...
- java颜色对照表