Leetcode 823. Binary Trees With Factors
Given an array of unique integers, arr
, where each integer arr[i]
is greater than 1, we want to determine the number of binary trees we can form. Each integer in the array can be a tree node, and for each possible tree node, the tree’s value at that node must equal the product of elements from the array present in its left and right subtrees. Return the number of possible binary trees modulo (10^9 + 7).
Before we dive into solving the problem, let’s clarify a few points:
Given these, let’s outline the strategy to derive the solution.
x
in the sorted array, iterate through all smaller numbers y
. If x % y == 0
and x / y
is also in the array, this implies y * (x / y) = x
, we can form a tree with y
as one subtree and x / y
as the other.The time complexity will primarily be driven by the nested iteration through pairs of numbers, which gives us:
Given the constraints, this approach should be efficient enough.
#include <vector>
#include <unordered_map>
#include <algorithm>
class Solution {
public:
int numFactoredBinaryTrees(std::vector<int>& arr) {
const int MOD = 1'000'000'007;
std::sort(arr.begin(), arr.end());
std::unordered_map<int, long> dp;
long result = 0;
for (int i = 0; i < arr.size(); ++i) {
dp[arr[i]] = 1; // Each number can form a tree by itself
for (int j = 0; j < i; ++j) {
if (arr[i] % arr[j] == 0) {
int other = arr[i] / arr[j];
if (dp.find(other) != dp.end()) {
dp[arr[i]] = (dp[arr[i]] + dp[arr[j]] * dp[other]) % MOD;
}
}
}
result = (result + dp[arr[i]]) % MOD;
}
return (int)result;
}
};
Here we go through the array and use a map to store the number of ways each number can form a tree, iterating over possible pairs of factors. This solution leverages sorting and dynamic programming to efficiently compute the result.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?