diff options
author | terminaldweller <thabogre@gmail.com> | 2022-08-09 06:48:28 +0000 |
---|---|---|
committer | terminaldweller <thabogre@gmail.com> | 2022-08-09 06:48:28 +0000 |
commit | 6a5f0f381928ca5b54b1e01a6e46baeaa91cbaca (patch) | |
tree | b2086d2276d072ac0e635de14440fa86696909ca /823 | |
parent | update (diff) | |
download | leetcode-6a5f0f381928ca5b54b1e01a6e46baeaa91cbaca.tar.gz leetcode-6a5f0f381928ca5b54b1e01a6e46baeaa91cbaca.zip |
update
Diffstat (limited to '823')
-rwxr-xr-x | 823/main.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/823/main.py b/823/main.py new file mode 100755 index 0000000..debdec0 --- /dev/null +++ b/823/main.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 +# not mine + +from typing import List + + +class Solution: + def __init__(self): + self.memo = {} + self.num_list = {} + + def numFactoredBinaryTrees(self, arr: List[int]) -> int: + modulo: int = 10**9 + 7 + N: int = len(arr) + arr.sort() + dp = [1] * N + index = {x: i for i, x in enumerate(arr)} + + for i, x in enumerate(arr): + for j in range(i): + if x % arr[j] == 0: + right = x / arr[j] + if right in index: + dp[i] += dp[j] * dp[int(index[int(right)])] + dp[i] %= modulo + + return sum(dp) % modulo + + +def main(): + solution = Solution() + assert solution.numFactoredBinaryTrees([2, 4]), 3 + assert solution.numFactoredBinaryTrees([2, 4, 5, 10]), 7 + + +if __name__ == "__main__": + main() |