Skip to main content

823 - Binary Trees With Factors

Details

KeyValue
Linkhttps://leetcode.com/problems/binary-trees-with-factors/
LanguagePython 3
Runtime496 ms, faster than 76.26% of Python3 online submissions for Binary Trees With Factors
Memory Usage14.1 MB, less than 90.91% of Python3 online submissions for Binary Trees With Factors
DatastructuresDP
AlgorithmsGroup + Least Common Prefix?
ComplexityTime: O(N^2) Memory: O(N)

Procedure

  1. ...

Code

class Solution:
def numFactoredBinaryTrees(self, arr: List[int]) -> int:
arr.sort()
dp = {num: 1 for num in arr}
for i, a in enumerate(arr):
for b in arr[0:i]:
if a%b ==0:
key = int(a/b)
if key in dp:
dp[a] += dp[b]*dp[key]

return sum(dp.values()) % (10**9+7)