description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: A.sort() d = {A[0]: 0} dp = [1] * len(A) MOD = 1000000007 for i in range(1, len(A)): for j in range(0, i): t = A[i] // A[j] if t < A[j]: break if not A[i] % A[j] and t in d: if A[j] == t: dp[i] += dp[j] * dp[d[t]] else: dp[i] += dp[j] * dp[d[t]] * 2 dp[i] %= MOD d[A[i]] = i return sum(dp) % MOD
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: A.sort() t = {} for a in A: t[a] = 1 + sum(t[b] * t.get(a / b, 0) for b in A if b < a) return sum(t.values()) % (pow(10, 9) + 7)
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: MOD = 10**9 + 7 N = len(A) A.sort() dp = [1] * N index = {x: i for i, x in enumerate(A)} for i, x in enumerate(A): for j in range(i): if x % A[j] == 0: right = x / A[j] if right in index: dp[i] += dp[j] * dp[index[right]] dp[i] %= MOD return sum(dp) % MOD
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: A = sorted(A) mems = {} for ix in range(len(A)): mems[A[ix]] = ix dp = [1] * len(A) big_mod = 10**9 + 7 for i in range(len(A)): extras = 0 for j in range(i): d, mo = divmod(A[i], A[j]) if mo == 0: if d in mems: extras += dp[j] * dp[mems[d]] extras = extras % big_mod dp[i] += extras result = sum(dp) % big_mod return result
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: mod = 10**9 + 7 A.sort() parents = dict() seen = set() for a in A: tot = 1 for s in seen: if a % s == 0: b = a // s if b in seen: tot += parents[s] * parents[b] parents[a] = tot seen.add(a) return sum(parents.values()) % mod
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: if not A: return 0 A.sort() dp = {} for i in range(len(A)): dp[A[i]] = 1 for j in range(i): if A[i] % A[j] == 0 and int(A[i] / A[j]) in dp: dp[A[i]] += dp[A[j]] * dp[A[i] / A[j]] result = 0 for key in dp: result += dp[key] return result % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: memo = {} ans = 0 A.sort() for i in range(len(A)): localAns = 1 for j in range(i, -1, -1): if A[i] / A[j] in memo: localAns += memo[A[i] / A[j]] * memo[A[j]] ans += localAns memo[A[i]] = localAns return ans % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: A.sort() d = {} MOD = 1000000007 for a in A: d[a] = 1 + sum(d[a // b] * d[b] for b in d if not a % b and a // b in d) return sum(d.values()) % MOD
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: A.sort() dp = {} for i, a in enumerate(A): dp[a] = 1 for j in range(i): if a % A[j] == 0 and a / A[j] in dp: dp[a] += dp[A[j]] * dp[a / A[j]] return sum(dp.values()) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: seen, A = {}, set(A) def find_all(root): if root in seen: return seen[root] combs = 1 for left in A: if root == left or root % left: continue right = root // left if right not in A: continue combs += find_all(left) * find_all(right) seen[root] = combs return combs return sum(find_all(num) for num in sorted(A)) % 1000000007
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR DICT FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: A.sort() A_set = set(A) cache = {} num_trees = 0 for i, n in enumerate(A): factor_pairs = [] for j in range(i): if n % A[j] == 0 and n // A[j] in A_set: factor_pairs.append((n // A[j], A[j])) new_trees = 1 for factor1, factor2 in factor_pairs: if factor1 != factor2: new_trees += cache[factor1] * cache[factor2] elif factor1 == factor2: new_trees += cache[factor1] * cache[factor2] num_trees += new_trees cache[n] = new_trees print(cache) return num_trees % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: A.sort() n = len(A) dp = [(1) for i in range(n)] for i in range(n): start, end = 0, i - 1 while start <= end: if A[start] * A[end] < A[i]: start += 1 continue if A[start] * A[end] > A[i]: end -= 1 continue if start == end: dp[i] += dp[start] * dp[end] else: dp[i] += dp[start] * dp[end] * 2 start += 1 end -= 1 return sum(dp) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: d = {} Aset = set(A) A.sort() ans = 0 for i in range(len(A)): d[A[i]] = 1 for j in range(i): if A[i] % A[j] == 0: k = A[i] // A[j] if k in set(A): d[A[i]] += d[A[j]] * d[k] d[A[i]] %= 10**9 + 7 ans += d[A[i]] ans %= 10**9 + 7 return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: A.sort() ans = 0 N = len(A) cnt = [(1) for _ in range(N)] for i, n in enumerate(A): l, r = 0, i - 1 while r >= l: if A[r] * A[l] == n: cnt[i] += cnt[r] * cnt[l] if r == l else cnt[r] * cnt[l] * 2 l += 1 r -= 1 elif A[r] * A[l] > n: r -= 1 else: l += 1 ans += cnt[i] return ans % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, x: List[int]) -> int: def f(n): if n in dp: return dp[n] ans = 1 for i in x: if i > n**0.5: break if n % i == 0 and n // i in se: ans += (1 if n // i == i else 2) * f(i) * f(n // i) dp[n] = ans return ans dp = {} x.sort() se = set(x) ans = 0 for i in x: ans += f(i) return ans % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR DICT EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: mod = 10**9 + 7 processed = dict() res = 0 for a in sorted(A): counter = 1 for key, val in list(processed.items()): key2 = a / key val2 = processed.get(key2, 0) if val2 != 0: counter = (counter + val * val2) % mod processed[a] = counter res = (res + counter) % mod return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: A.sort() O = {} for a in A: if a not in O: O[a] = 1 for k, v in list(O.items()): if a % k == 0 and a // k in O: O[a] += v * O[a // k] return sum(O.values()) % 1000000007
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: n = len(A) A.sort() s = {a: i for i, a in enumerate(A)} d = collections.defaultdict(set) for i in range(n): for j in range(i): if A[i] % A[j] == 0: res = A[i] // A[j] if res in s: d[i].add((j, s[res])) @lru_cache(maxsize=None) def dfs(i): nonlocal ans cur = 1 for l, r in d[i]: cur += dfs(l) * dfs(r) return cur ans = 0 for i in range(n): val = dfs(i) ans += val return ans % 1000000007
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_CALL VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: A.sort() dp = {} res = 0 for i in range(len(A)): dp[A[i]] = 1 for j in range(i): if A[j] * A[j] == A[i]: dp[A[i]] += dp[A[j]] * dp[A[j]] dp[A[i]] %= 1000000007 elif A[j] * A[j] > A[i]: div = A[i] // A[j] if div * A[j] == A[i] and div in dp: dp[A[i]] += dp[A[j]] * dp[div] * 2 dp[A[i]] %= 1000000007 res += dp[A[i]] res %= 1000000007 return res
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: cache = dict() iSet = set(A) def helper(i): if i not in cache: count = 1 for j in range(len(A)): if i != A[j] and i % A[j] == 0: if i // A[j] in iSet: count += helper(A[j]) * helper(i // A[j]) cache[i] = count return cache[i] ans = 0 for i in range(len(A)): ans += helper(A[i]) return ans % 1000000007
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: factorSet = set(A) A.sort() treeCount = collections.defaultdict(lambda: 1) n = len(A) end = 0 for num in A: sqrt = int(math.sqrt(num) + 1) while end < n and A[end] <= sqrt: end += 1 count = 0 for _, p in zip(list(range(end)), A): if num % p == 0 and (q := num // p) in factorSet and p <= q: count += ( 2 * treeCount[p] * treeCount[q] if p != q else treeCount[p] * treeCount[q] ) treeCount[num] += count return sum(treeCount.values()) % (1000000000 + 7)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: A.sort() dp = {n: (1) for n in A} setA = set(A) for i in range(len(A)): target = A[i] for j in range(0, i): factor = A[j] if target % factor == 0: other_half = target // factor if other_half in setA: dp[target] += dp[other_half] * dp[factor] return sum(dp.values()) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: if len(A) <= 1: return len(A) N, MOD = len(A), 10**9 + 7 trees = {} ans = 0 A.sort() for i in range(N): trees[A[i]] = 1 for j in range(i): factor = A[i] / A[j] if not factor.is_integer(): continue factor = int(factor) if factor in trees: trees[A[i]] += trees[A[j]] * trees[factor] ans += trees[A[i]] return ans % MOD
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN BIN_OP VAR VAR VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: n = len(A) A.sort() dp = [1] * n mod = 10**9 + 7 index = {c: i for i, c in enumerate(A)} for i, x in enumerate(A): for j in range(n): if x % A[j] == 0: right = x / A[j] if right in index: dp[i] += dp[j] * dp[index[right]] dp[i] = dp[i] % mod return sum(dp) % mod
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: A.sort() dp = {} mod = 10**9 + 7 for a in A: dp[a] = 1 for b in A: if a % b == 0 and b in dp and a // b in dp: dp[a] += dp[b] * dp[a // b] dp[a] %= mod return sum(dp.values()) % mod
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: dp = {} for a in sorted(A): dp[a] = sum(dp[b] * dp.get(a // b, 0) for b in dp if a % b == 0) + 1 return sum(dp.values()) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
def prodcount(a): dp = defaultdict(int) a.sort() for i in range(0, len(a)): dp[a[i]] = 1 for j in range(i): if a[i] % a[j] == 0: if a[i] // a[j] == a[j]: dp[a[i]] += dp[a[j]] * dp[a[i] // a[j]] elif dp[a[i] // a[j]]: dp[a[i]] += dp[a[j]] * dp[a[i] // a[j]] return sum(dp.values()) % (10**9 + 7) class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: return prodcount(A)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: n = len(A) nums = set(A) A = sorted(A) counts = collections.defaultdict(int) mod = 10**9 + 7 res = 0 for i, num in enumerate(A): for j in range(i): b, c = A[j], num / A[j] if c in nums: counts[num] += counts[b] * counts[c] % mod counts[num] += 1 res = (res + counts[num]) % mod return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR RETURN VAR VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: n = len(A) A.sort() dp = [1] * n res = 0 for i in range(n): s = 0 e = i - 1 for s in range(i): while s <= e and A[s] * A[e] > A[i]: e -= 1 if s > e: break if A[s] * A[e] == A[i]: dp[i] += dp[s] * dp[e] * 2 if s != e else dp[s] * dp[e] dp[i] = dp[i] % (10**9 + 7) e -= 1 res += dp[i] res = res % (10**9 + 7) return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: A.sort() dp = {i: (1) for i in A} res = 0 for idd, number in enumerate(A): for inner, inumber in enumerate(A[:idd]): if number % inumber == 0 and number // inumber in dp: dp[number] += dp[inumber] * dp[number // inumber] res += dp[number] res = res % (10**9 + 7) return res
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def collect(self, lst, st, num, cache): if num in cache: return cache[num] res = 1 for n1 in lst: if n1**2 > num: break if num % n1 == 0 and num // n1 in st: n2 = num // n1 res_local = self.collect(lst, st, n1, cache) * self.collect( lst, st, n2, cache ) res += res_local if n1 != n2: res += res_local mod = 10**9 + 7 res = res % mod cache[num] = res return res def solve(self, A): st = set(A) lst = sorted(list(st)) res = 0 cache = {} mod = 10**9 + 7 for n in lst: res += self.collect(lst, st, n, cache) return res % mod def numFactoredBinaryTrees(self, A: List[int]) -> int: return self.solve(A)
CLASS_DEF FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: factors = set(A) A.sort() dp = defaultdict(int) count = 0 modulus = 10**9 + 7 for i in range(len(A)): dp[A[i]] += 1 for j in range(i): if A[i] % A[j] == 0 and A[i] // A[j] in factors: dp[A[i]] += dp[A[j]] * dp[A[i] // A[j]] count = (count + dp[A[i]]) % modulus return count
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR RETURN VAR VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: num_trees = {} A.sort() A_set = set(A) for i in range(len(A)): curr_num = A[i] num_trees[curr_num] = 1 for j in range(i): if A[i] % A[j] == 0: if A[i] // A[j] in A_set: num_trees[curr_num] += num_trees[A[j]] * num_trees[A[i] // A[j]] total = 0 for key in num_trees: total += num_trees[key] total %= 10**9 + 7 return total
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: A.sort() n = len(A) dp = defaultdict(int) res = 0 for i, num in enumerate(A): dp[num] = 1 for j in range(i): if num % A[j] == 0: dp[num] += dp[A[j]] * dp[A[i] // A[j]] res += dp[num] return res % 1000000007
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: A.sort() ret = 0 root_value_to_tree_number = collections.defaultdict(int) for i in range(len(A)): occurrences = 1 for j in range(i - 1, -1, -1): if A[i] % A[j] == 0: required_root_value = A[i] // A[j] occurrences += ( root_value_to_tree_number[required_root_value] * root_value_to_tree_number[A[j]] ) root_value_to_tree_number[A[i]] = occurrences ret += occurrences return ret % 1000000007
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: mod = 10**9 + 7 nums_set = set(A) nums = A.copy() nums.sort() counts = {} total = 0 for n in nums: n_count = 1 for d in nums: if d * d > n: break if n % d != 0: continue e = n // d if e not in nums_set: continue subtrees = counts[d] * counts[e] % mod if d != e: subtrees = subtrees * 2 % mod n_count = (n_count + subtrees) % mod counts[n] = n_count % mod total = (total + n_count) % mod return total
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: A.sort() dp = [(1) for _ in range(len(A))] for i in range(len(A)): for j in range(i): time, remain = divmod(A[i], A[j]) if remain == 0 and time in A: dp[i] += dp[j] * dp[A.index(time)] return sum(dp) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: factorSet = set(A) A.sort() treeCount = {} for num in A: count = 1 for p, pCount in list(treeCount.items()): q, rem = divmod(num, p) if p > q: break if rem == 0: tmp = pCount * treeCount.get(q, 0) count += tmp if p == q else 2 * tmp treeCount[num] = count return sum(treeCount.values()) % (1000000000 + 7)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: SA = set(A) A = sorted(SA) C = {} for x in A: C[x] = 1 for i, x in enumerate(A): for j in range(i): if A[j] * A[j] > A[i]: break elif A[j] * A[j] == A[i]: C[A[i]] += C[A[j]] * C[A[j]] elif A[i] % A[j] == 0 and A[i] // A[j] in SA: C[A[i]] += 2 * C[A[j]] * C[A[i] // A[j]] res = sum(C.values()) return res % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: A = sorted(A) n = len(A) dict_num = {} for num in A: dict_num[num] = True dp_dict = {} factor_pair = [] for i in range(n): factor_pair.append([]) for i in range(n): for j in range(i): if A[i] % A[j] == 0 and A[i] // A[j] in dict_num: factor_pair[i].append((A[j], A[i] // A[j])) for i in range(n): root = A[i] num_trees = 1 for a, b in factor_pair[i]: num_trees += dp_dict[a] * dp_dict[b] dp_dict[root] = num_trees answer = 0 for key in dp_dict: answer += dp_dict[key] return answer % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
MOD = 10**9 + 7 def count_binary_trees(arr): arr = sorted(arr) coll = set(arr) count = {} for index, elem in enumerate(arr): ans = 1 for i in range(index): factor = arr[i] if elem % factor == 0 and elem // factor in coll: other_factor = elem // factor ans = (ans + count[factor] * count[other_factor]) % MOD count[elem] = ans return sum(y for x, y in count.items()) % MOD class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: return count_binary_trees(A)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: A.sort() dp = {val: (1) for val in A} for i in range(len(A)): for j in range(i): if A[i] % A[j] == 0 and A[i] // A[j] in dp: dp[A[i]] += dp[A[i] // A[j]] * dp[A[j]] return sum(dp[v] for v in dp) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: A.sort() mapping = {x: i for i, x in enumerate(A)} maximal = A[-1] N = len(A) ans, MOD = [1] * N, 10**9 + 7 for i in range(N): for j in range(i + 1): if A[i] * A[j] in mapping and A[i] * A[j] <= maximal: if A[i] != A[j]: ans[mapping[A[i] * A[j]]] += 2 * ans[i] * ans[j] else: ans[mapping[A[i] * A[j]]] += ans[i] * ans[j] ans[mapping[A[i] * A[j]]] %= MOD return sum(ans) % MOD
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: A = sorted(A) N = len(A) dp = [(1) for _ in range(N)] index_map = {} for index in range(N): index_map[A[index]] = index for k in range(N): for i in range(k): if A[k] % A[i] == 0: num_to_look = A[k] // A[i] if num_to_look in index_map: j = index_map[num_to_look] dp[k] += dp[i] * dp[j] return sum(dp) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: MOD = 10**9 + 7 n = len(A) tips = sorted(A) index = {x: i for i, x in enumerate(tips)} d = [1] * n for i, x in enumerate(tips): head = x for j in range(i): x = tips[j] y = head // x if y * x != head: continue if y in index: leaf_1 = j leaf_2 = index[y] d[i] = d[i] + d[leaf_1] * d[leaf_2] return sum(d) % MOD
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR
Penelope and her classmates are lost in the Forbidden Forest and the Devil is out to get them. But Penelope has magical powers that can build bridges across the dangerous river and take her friends to safety. The only bridges that can withstand the Devil's wrath are the ones built between two similar trees in the forest. Given str1 and str2 denoting the order of trees on either side of the river, find the maximum number of bridges that Penelope can build and save everyone from the Devil. Note: Each tree in the forest belongs to one of the 3 categories represented by * or # or @, and it's easy to understand that bridges do not intersect or cross each other. Example 1: Input: str1 = "*@#*" str2 = "*#" Output: 2 Explanation: str1 = "*@#*" and str2 = "*#" Two bridges can be built between the banks of the river in the following manner. * @ # * | | * # Example 2: Input: str1 = "***" str2 = "##" Output: 0 Your Task: You don't need to read input or print anything. Complete the function build_bridges() that takes str1 and str2 as input parameters and returns the maximum number of bridges that can be built. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(N*M) Constraints: 1 ≤ N, M ≤ 100 Where, N and M are the size of the string str1 and str2 respectively.
class Solution: def build_bridges(self, str1, str2): n = len(str1) m = len(str2) F = [([0] * (n + 1)) for _ in range(m + 1)] for i in range(m + 1): for j in range(n + 1): if j == 0 or i == 0: F[i][j] = 0 elif str1[j - 1] == str2[i - 1]: F[i][j] = F[i - 1][j - 1] + 1 else: F[i][j] = max(F[i - 1][j], F[i][j - 1]) return F[m][n]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR
Penelope and her classmates are lost in the Forbidden Forest and the Devil is out to get them. But Penelope has magical powers that can build bridges across the dangerous river and take her friends to safety. The only bridges that can withstand the Devil's wrath are the ones built between two similar trees in the forest. Given str1 and str2 denoting the order of trees on either side of the river, find the maximum number of bridges that Penelope can build and save everyone from the Devil. Note: Each tree in the forest belongs to one of the 3 categories represented by * or # or @, and it's easy to understand that bridges do not intersect or cross each other. Example 1: Input: str1 = "*@#*" str2 = "*#" Output: 2 Explanation: str1 = "*@#*" and str2 = "*#" Two bridges can be built between the banks of the river in the following manner. * @ # * | | * # Example 2: Input: str1 = "***" str2 = "##" Output: 0 Your Task: You don't need to read input or print anything. Complete the function build_bridges() that takes str1 and str2 as input parameters and returns the maximum number of bridges that can be built. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(N*M) Constraints: 1 ≤ N, M ≤ 100 Where, N and M are the size of the string str1 and str2 respectively.
class Solution: def build_bridges(self, str1, str2): return self.helper(str1, str2, {}) def helper(self, s1, s2, store): if not s1 or not s2: return 0 if (s1, s2) in store: return store[s1, s2] if s1[0] == s2[0]: store[s1, s2] = self.helper(s1[1:], s2[1:], store) + 1 return store[s1, s2] first = self.helper(s1[1:], s2, store) second = self.helper(s1, s2[1:], store) store[s1, s2] = max(first, second) return store[s1, s2]
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR DICT FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR
Penelope and her classmates are lost in the Forbidden Forest and the Devil is out to get them. But Penelope has magical powers that can build bridges across the dangerous river and take her friends to safety. The only bridges that can withstand the Devil's wrath are the ones built between two similar trees in the forest. Given str1 and str2 denoting the order of trees on either side of the river, find the maximum number of bridges that Penelope can build and save everyone from the Devil. Note: Each tree in the forest belongs to one of the 3 categories represented by * or # or @, and it's easy to understand that bridges do not intersect or cross each other. Example 1: Input: str1 = "*@#*" str2 = "*#" Output: 2 Explanation: str1 = "*@#*" and str2 = "*#" Two bridges can be built between the banks of the river in the following manner. * @ # * | | * # Example 2: Input: str1 = "***" str2 = "##" Output: 0 Your Task: You don't need to read input or print anything. Complete the function build_bridges() that takes str1 and str2 as input parameters and returns the maximum number of bridges that can be built. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(N*M) Constraints: 1 ≤ N, M ≤ 100 Where, N and M are the size of the string str1 and str2 respectively.
class Solution: def build_bridges(self, str1, str2): dp = [] for i in range(len(str2) + 1): dp.append([0] * (len(str1) + 1)) for j in range(len(str1) + 1): if i > 0 and j > 0: if str2[i - 1] == str1[j - 1]: dp[i][j] = dp[i - 1][j - 1] + 1 else: dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) return dp[-1][-1]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER NUMBER
Penelope and her classmates are lost in the Forbidden Forest and the Devil is out to get them. But Penelope has magical powers that can build bridges across the dangerous river and take her friends to safety. The only bridges that can withstand the Devil's wrath are the ones built between two similar trees in the forest. Given str1 and str2 denoting the order of trees on either side of the river, find the maximum number of bridges that Penelope can build and save everyone from the Devil. Note: Each tree in the forest belongs to one of the 3 categories represented by * or # or @, and it's easy to understand that bridges do not intersect or cross each other. Example 1: Input: str1 = "*@#*" str2 = "*#" Output: 2 Explanation: str1 = "*@#*" and str2 = "*#" Two bridges can be built between the banks of the river in the following manner. * @ # * | | * # Example 2: Input: str1 = "***" str2 = "##" Output: 0 Your Task: You don't need to read input or print anything. Complete the function build_bridges() that takes str1 and str2 as input parameters and returns the maximum number of bridges that can be built. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(N*M) Constraints: 1 ≤ N, M ≤ 100 Where, N and M are the size of the string str1 and str2 respectively.
class Solution: def build_bridges(self, str1, str2): d = dict() def lcs(str1, str2, n, m): if n == 0 or m == 0: return 0 if (n, m) in d: return d[n, m] if str1[n - 1] == str2[m - 1]: return 1 + lcs(str1, str2, n - 1, m - 1) d[n, m] = max(lcs(str1, str2, n, m - 1), lcs(str1, str2, n - 1, m)) return d[n, m] return lcs(str1, str2, len(str1), len(str2))
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Penelope and her classmates are lost in the Forbidden Forest and the Devil is out to get them. But Penelope has magical powers that can build bridges across the dangerous river and take her friends to safety. The only bridges that can withstand the Devil's wrath are the ones built between two similar trees in the forest. Given str1 and str2 denoting the order of trees on either side of the river, find the maximum number of bridges that Penelope can build and save everyone from the Devil. Note: Each tree in the forest belongs to one of the 3 categories represented by * or # or @, and it's easy to understand that bridges do not intersect or cross each other. Example 1: Input: str1 = "*@#*" str2 = "*#" Output: 2 Explanation: str1 = "*@#*" and str2 = "*#" Two bridges can be built between the banks of the river in the following manner. * @ # * | | * # Example 2: Input: str1 = "***" str2 = "##" Output: 0 Your Task: You don't need to read input or print anything. Complete the function build_bridges() that takes str1 and str2 as input parameters and returns the maximum number of bridges that can be built. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(N*M) Constraints: 1 ≤ N, M ≤ 100 Where, N and M are the size of the string str1 and str2 respectively.
class Solution: def build_bridges(self, text1, text2): len1, len2 = len(text1), len(text2) dp = [[(0) for i in range(len2)] for i in range(len1)] for i in range(len1): for j in range(len2): if text1[i] == text2[j]: if i == 0 or j == 0: dp[i][j] = 1 else: dp[i][j] = 1 + dp[i - 1][j - 1] else: dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) return dp[-1][-1]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER NUMBER
Penelope and her classmates are lost in the Forbidden Forest and the Devil is out to get them. But Penelope has magical powers that can build bridges across the dangerous river and take her friends to safety. The only bridges that can withstand the Devil's wrath are the ones built between two similar trees in the forest. Given str1 and str2 denoting the order of trees on either side of the river, find the maximum number of bridges that Penelope can build and save everyone from the Devil. Note: Each tree in the forest belongs to one of the 3 categories represented by * or # or @, and it's easy to understand that bridges do not intersect or cross each other. Example 1: Input: str1 = "*@#*" str2 = "*#" Output: 2 Explanation: str1 = "*@#*" and str2 = "*#" Two bridges can be built between the banks of the river in the following manner. * @ # * | | * # Example 2: Input: str1 = "***" str2 = "##" Output: 0 Your Task: You don't need to read input or print anything. Complete the function build_bridges() that takes str1 and str2 as input parameters and returns the maximum number of bridges that can be built. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(N*M) Constraints: 1 ≤ N, M ≤ 100 Where, N and M are the size of the string str1 and str2 respectively.
class Solution: def build_bridges(self, s1, s2): n = len(s1) m = len(s2) dp = [([0] * (m + 1)) for i in range(n + 1)] for i in range(1, n + 1): for j in range(1, m + 1): if i == 0 or j == 0: continue if s1[i - 1] == s2[j - 1]: dp[i][j] = dp[i - 1][j - 1] + 1 else: dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]) return dp[n][m] if __name__ == "__main__": T = int(input()) for _ in range(T): obj = Solution() str1, str2 = input().split() print(obj.build_bridges(str1, str2))
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Penelope and her classmates are lost in the Forbidden Forest and the Devil is out to get them. But Penelope has magical powers that can build bridges across the dangerous river and take her friends to safety. The only bridges that can withstand the Devil's wrath are the ones built between two similar trees in the forest. Given str1 and str2 denoting the order of trees on either side of the river, find the maximum number of bridges that Penelope can build and save everyone from the Devil. Note: Each tree in the forest belongs to one of the 3 categories represented by * or # or @, and it's easy to understand that bridges do not intersect or cross each other. Example 1: Input: str1 = "*@#*" str2 = "*#" Output: 2 Explanation: str1 = "*@#*" and str2 = "*#" Two bridges can be built between the banks of the river in the following manner. * @ # * | | * # Example 2: Input: str1 = "***" str2 = "##" Output: 0 Your Task: You don't need to read input or print anything. Complete the function build_bridges() that takes str1 and str2 as input parameters and returns the maximum number of bridges that can be built. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(N*M) Constraints: 1 ≤ N, M ≤ 100 Where, N and M are the size of the string str1 and str2 respectively.
class Solution: def lcs(self, X, Y, m, n): t = [[(0) for i in range(n + 1)] for j in range(m + 1)] for i in range(m + 1): for j in range(n + 1): if i == 0 or j == 0: t[i][j] = 0 elif X[i - 1] == Y[j - 1]: t[i][j] = 1 + t[i - 1][j - 1] else: t[i][j] = max(t[i - 1][j], t[i][j - 1]) return t[m][n] def build_bridges(self, str1, str2): m = len(str1) n = len(str2) return self.lcs(str1, str2, m, n)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR
Penelope and her classmates are lost in the Forbidden Forest and the Devil is out to get them. But Penelope has magical powers that can build bridges across the dangerous river and take her friends to safety. The only bridges that can withstand the Devil's wrath are the ones built between two similar trees in the forest. Given str1 and str2 denoting the order of trees on either side of the river, find the maximum number of bridges that Penelope can build and save everyone from the Devil. Note: Each tree in the forest belongs to one of the 3 categories represented by * or # or @, and it's easy to understand that bridges do not intersect or cross each other. Example 1: Input: str1 = "*@#*" str2 = "*#" Output: 2 Explanation: str1 = "*@#*" and str2 = "*#" Two bridges can be built between the banks of the river in the following manner. * @ # * | | * # Example 2: Input: str1 = "***" str2 = "##" Output: 0 Your Task: You don't need to read input or print anything. Complete the function build_bridges() that takes str1 and str2 as input parameters and returns the maximum number of bridges that can be built. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(N*M) Constraints: 1 ≤ N, M ≤ 100 Where, N and M are the size of the string str1 and str2 respectively.
class Solution: def build_bridges(self, str1, str2): dp = [] for i in range(len(str1)): dp.append([(-1) for _ in range(len(str2))]) def lcs(i, j, c1, c2): nonlocal dp if i == len(c1) or j == len(c2): return 0 if dp[i][j] != -1: return dp[i][j] if c1[i] == c2[j]: return 1 + lcs(i + 1, j + 1, c1, c2) else: dp[i][j] = max(lcs(i + 1, j, c1, c2), lcs(i, j + 1, c1, c2)) return dp[i][j] return lcs(0, 0, str1, str2)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR RETURN BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR VAR
Penelope and her classmates are lost in the Forbidden Forest and the Devil is out to get them. But Penelope has magical powers that can build bridges across the dangerous river and take her friends to safety. The only bridges that can withstand the Devil's wrath are the ones built between two similar trees in the forest. Given str1 and str2 denoting the order of trees on either side of the river, find the maximum number of bridges that Penelope can build and save everyone from the Devil. Note: Each tree in the forest belongs to one of the 3 categories represented by * or # or @, and it's easy to understand that bridges do not intersect or cross each other. Example 1: Input: str1 = "*@#*" str2 = "*#" Output: 2 Explanation: str1 = "*@#*" and str2 = "*#" Two bridges can be built between the banks of the river in the following manner. * @ # * | | * # Example 2: Input: str1 = "***" str2 = "##" Output: 0 Your Task: You don't need to read input or print anything. Complete the function build_bridges() that takes str1 and str2 as input parameters and returns the maximum number of bridges that can be built. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(N*M) Constraints: 1 ≤ N, M ≤ 100 Where, N and M are the size of the string str1 and str2 respectively.
class Solution: dp = [] def build_bridges(self, str1, str2): l1 = len(str1) l2 = len(str2) Solution.dp = [[(-1) for i in range(l2)] for j in range(l1)] res = Solution.bridges(l1 - 1, l2 - 1, str1, str2) return res def bridges(n, m, s1, s2): if n < 0 or m < 0: return 0 if (n == 0 or m == 0) and s1[n] == s2[m]: return 1 if Solution.dp[n][m] == -1: if s1[n] == s2[m]: Solution.dp[n - 1][m - 1] = Solution.bridges(n - 1, m - 1, s1, s2) return 1 + Solution.dp[n - 1][m - 1] else: Solution.dp[n][m] = max( Solution.bridges(n - 1, m, s1, s2), Solution.bridges(n, m - 1, s1, s2), ) return Solution.dp[n][m] else: return Solution.dp[n][m]
CLASS_DEF ASSIGN VAR LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR RETURN VAR VAR VAR
Penelope and her classmates are lost in the Forbidden Forest and the Devil is out to get them. But Penelope has magical powers that can build bridges across the dangerous river and take her friends to safety. The only bridges that can withstand the Devil's wrath are the ones built between two similar trees in the forest. Given str1 and str2 denoting the order of trees on either side of the river, find the maximum number of bridges that Penelope can build and save everyone from the Devil. Note: Each tree in the forest belongs to one of the 3 categories represented by * or # or @, and it's easy to understand that bridges do not intersect or cross each other. Example 1: Input: str1 = "*@#*" str2 = "*#" Output: 2 Explanation: str1 = "*@#*" and str2 = "*#" Two bridges can be built between the banks of the river in the following manner. * @ # * | | * # Example 2: Input: str1 = "***" str2 = "##" Output: 0 Your Task: You don't need to read input or print anything. Complete the function build_bridges() that takes str1 and str2 as input parameters and returns the maximum number of bridges that can be built. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(N*M) Constraints: 1 ≤ N, M ≤ 100 Where, N and M are the size of the string str1 and str2 respectively.
class Solution: def lcs(self, str1, str2, n1, n2): if n1 == 0 or n2 == 0: return 0 elif str1[n1 - 1] == str2[n2 - 1]: return 1 + self.lcs(str1, str2, n1 - 1, n2 - 1) else: return max( self.lcs(str1, str2, n1, n2 - 1), self.lcs(str1, str2, n1 - 1, n2) ) def build_bridges(self, str1, str2): dp = [([0] * len(str2)) for i in range(len(str1))] for i in range(len(str1)): for j in range(len(str2)): if str1[i] == str2[j]: if i == 0 or j == 0: dp[i][j] = 1 else: dp[i][j] = 1 + dp[i - 1][j - 1] else: dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) return dp[-1][-1] if __name__ == "__main__": T = int(input()) for _ in range(T): obj = Solution() str1, str2 = input().split() print(obj.build_bridges(str1, str2))
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Penelope and her classmates are lost in the Forbidden Forest and the Devil is out to get them. But Penelope has magical powers that can build bridges across the dangerous river and take her friends to safety. The only bridges that can withstand the Devil's wrath are the ones built between two similar trees in the forest. Given str1 and str2 denoting the order of trees on either side of the river, find the maximum number of bridges that Penelope can build and save everyone from the Devil. Note: Each tree in the forest belongs to one of the 3 categories represented by * or # or @, and it's easy to understand that bridges do not intersect or cross each other. Example 1: Input: str1 = "*@#*" str2 = "*#" Output: 2 Explanation: str1 = "*@#*" and str2 = "*#" Two bridges can be built between the banks of the river in the following manner. * @ # * | | * # Example 2: Input: str1 = "***" str2 = "##" Output: 0 Your Task: You don't need to read input or print anything. Complete the function build_bridges() that takes str1 and str2 as input parameters and returns the maximum number of bridges that can be built. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(N*M) Constraints: 1 ≤ N, M ≤ 100 Where, N and M are the size of the string str1 and str2 respectively.
class Solution: def build_bridges(self, str1, str2): m, n = len(str1), len(str2) prev, cur = [0] * (n + 1), [0] * (n + 1) for i in range(1, m + 1): for j in range(1, n + 1): if str1[i - 1] == str2[j - 1]: cur[j] = 1 + prev[j - 1] elif cur[j - 1] > prev[j]: cur[j] = cur[j - 1] else: cur[j] = prev[j] cur, prev = prev, cur return prev[n]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR
Penelope and her classmates are lost in the Forbidden Forest and the Devil is out to get them. But Penelope has magical powers that can build bridges across the dangerous river and take her friends to safety. The only bridges that can withstand the Devil's wrath are the ones built between two similar trees in the forest. Given str1 and str2 denoting the order of trees on either side of the river, find the maximum number of bridges that Penelope can build and save everyone from the Devil. Note: Each tree in the forest belongs to one of the 3 categories represented by * or # or @, and it's easy to understand that bridges do not intersect or cross each other. Example 1: Input: str1 = "*@#*" str2 = "*#" Output: 2 Explanation: str1 = "*@#*" and str2 = "*#" Two bridges can be built between the banks of the river in the following manner. * @ # * | | * # Example 2: Input: str1 = "***" str2 = "##" Output: 0 Your Task: You don't need to read input or print anything. Complete the function build_bridges() that takes str1 and str2 as input parameters and returns the maximum number of bridges that can be built. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(N*M) Constraints: 1 ≤ N, M ≤ 100 Where, N and M are the size of the string str1 and str2 respectively.
class Solution: def build_bridges(self, str1, str2): d = [([0] * (len(str2) + 1)) for i in range(len(str1) + 1)] for i in range(1, len(str1) + 1): for j in range(1, len(str2) + 1): if str1[i - 1] == str2[j - 1]: d[i][j] = 1 + d[i - 1][j - 1] else: d[i][j] = max(d[i - 1][j], d[i][j - 1]) return d[-1][-1]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER NUMBER
Penelope and her classmates are lost in the Forbidden Forest and the Devil is out to get them. But Penelope has magical powers that can build bridges across the dangerous river and take her friends to safety. The only bridges that can withstand the Devil's wrath are the ones built between two similar trees in the forest. Given str1 and str2 denoting the order of trees on either side of the river, find the maximum number of bridges that Penelope can build and save everyone from the Devil. Note: Each tree in the forest belongs to one of the 3 categories represented by * or # or @, and it's easy to understand that bridges do not intersect or cross each other. Example 1: Input: str1 = "*@#*" str2 = "*#" Output: 2 Explanation: str1 = "*@#*" and str2 = "*#" Two bridges can be built between the banks of the river in the following manner. * @ # * | | * # Example 2: Input: str1 = "***" str2 = "##" Output: 0 Your Task: You don't need to read input or print anything. Complete the function build_bridges() that takes str1 and str2 as input parameters and returns the maximum number of bridges that can be built. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(N*M) Constraints: 1 ≤ N, M ≤ 100 Where, N and M are the size of the string str1 and str2 respectively.
class Solution: def solve(self, n, m): mat = [[(0) for i in range(m + 1)] for j in range(n + 1)] return mat def build_bridges(self, str1, str2): n = len(str1) m = len(str2) mat = self.solve(n, m) for i in range(1, n + 1): for j in range(1, m + 1): if str1[i - 1] == str2[j - 1]: mat[i][j] = 1 + mat[i - 1][j - 1] else: mat[i][j] = max(mat[i][j - 1], mat[i - 1][j]) if n == 0 or m == 0: return 0 else: return mat[n][m]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN VAR VAR VAR
Penelope and her classmates are lost in the Forbidden Forest and the Devil is out to get them. But Penelope has magical powers that can build bridges across the dangerous river and take her friends to safety. The only bridges that can withstand the Devil's wrath are the ones built between two similar trees in the forest. Given str1 and str2 denoting the order of trees on either side of the river, find the maximum number of bridges that Penelope can build and save everyone from the Devil. Note: Each tree in the forest belongs to one of the 3 categories represented by * or # or @, and it's easy to understand that bridges do not intersect or cross each other. Example 1: Input: str1 = "*@#*" str2 = "*#" Output: 2 Explanation: str1 = "*@#*" and str2 = "*#" Two bridges can be built between the banks of the river in the following manner. * @ # * | | * # Example 2: Input: str1 = "***" str2 = "##" Output: 0 Your Task: You don't need to read input or print anything. Complete the function build_bridges() that takes str1 and str2 as input parameters and returns the maximum number of bridges that can be built. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(N*M) Constraints: 1 ≤ N, M ≤ 100 Where, N and M are the size of the string str1 and str2 respectively.
class Solution: def build_bridges(self, str1, str2): lcs = [([0] * (len(str1) + 1)) for _ in range(len(str2) + 1)] for idx in range(len(str1) + 1): lcs[len(str2)][idx] = 0 for idx in range(len(str2) + 1): lcs[idx][len(str1)] = 0 for i in range(len(str2) - 1, -1, -1): for j in range(len(str1) - 1, -1, -1): if str1[j] == str2[i]: lcs[i][j] = 1 + lcs[i + 1][j + 1] else: lcs[i][j] = max(lcs[i][j + 1], lcs[i + 1][j]) return lcs[0][0]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER NUMBER
Penelope and her classmates are lost in the Forbidden Forest and the Devil is out to get them. But Penelope has magical powers that can build bridges across the dangerous river and take her friends to safety. The only bridges that can withstand the Devil's wrath are the ones built between two similar trees in the forest. Given str1 and str2 denoting the order of trees on either side of the river, find the maximum number of bridges that Penelope can build and save everyone from the Devil. Note: Each tree in the forest belongs to one of the 3 categories represented by * or # or @, and it's easy to understand that bridges do not intersect or cross each other. Example 1: Input: str1 = "*@#*" str2 = "*#" Output: 2 Explanation: str1 = "*@#*" and str2 = "*#" Two bridges can be built between the banks of the river in the following manner. * @ # * | | * # Example 2: Input: str1 = "***" str2 = "##" Output: 0 Your Task: You don't need to read input or print anything. Complete the function build_bridges() that takes str1 and str2 as input parameters and returns the maximum number of bridges that can be built. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(N*M) Constraints: 1 ≤ N, M ≤ 100 Where, N and M are the size of the string str1 and str2 respectively.
class Solution: def build_bridges(self, x, y): l = [] s1 = len(x) s2 = len(y) for i in range(0, s1 + 1): a = [] for j in range(0, s2 + 1): a.append(0) l.append(a) for i in range(0, s1 + 1): for j in range(0, s2 + 1): if i == 0 or j == 0: l[i][j] = 0 elif x[i - 1] == y[j - 1]: l[i][j] = l[i - 1][j - 1] + 1 else: l[i][j] = max(l[i][j - 1], l[i - 1][j]) return l[s1][s2]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR
Penelope and her classmates are lost in the Forbidden Forest and the Devil is out to get them. But Penelope has magical powers that can build bridges across the dangerous river and take her friends to safety. The only bridges that can withstand the Devil's wrath are the ones built between two similar trees in the forest. Given str1 and str2 denoting the order of trees on either side of the river, find the maximum number of bridges that Penelope can build and save everyone from the Devil. Note: Each tree in the forest belongs to one of the 3 categories represented by * or # or @, and it's easy to understand that bridges do not intersect or cross each other. Example 1: Input: str1 = "*@#*" str2 = "*#" Output: 2 Explanation: str1 = "*@#*" and str2 = "*#" Two bridges can be built between the banks of the river in the following manner. * @ # * | | * # Example 2: Input: str1 = "***" str2 = "##" Output: 0 Your Task: You don't need to read input or print anything. Complete the function build_bridges() that takes str1 and str2 as input parameters and returns the maximum number of bridges that can be built. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(N*M) Constraints: 1 ≤ N, M ≤ 100 Where, N and M are the size of the string str1 and str2 respectively.
class Solution: def build_bridges(self, str1, str2): N = len(str1) M = len(str2) dp = [[(0) for j in str2] for i in str1] for i in range(N): for j in range(M): a = (dp[i - 1][j - 1] if i > 0 and j > 0 else 0) + ( 1 if str2[j] == str1[i] else 0 ) b = dp[i - 1][j] if i > 0 else 0 c = dp[i][j - 1] if j > 0 else 0 dp[i][j] = max(a, b, c) return dp[N - 1][M - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
Penelope and her classmates are lost in the Forbidden Forest and the Devil is out to get them. But Penelope has magical powers that can build bridges across the dangerous river and take her friends to safety. The only bridges that can withstand the Devil's wrath are the ones built between two similar trees in the forest. Given str1 and str2 denoting the order of trees on either side of the river, find the maximum number of bridges that Penelope can build and save everyone from the Devil. Note: Each tree in the forest belongs to one of the 3 categories represented by * or # or @, and it's easy to understand that bridges do not intersect or cross each other. Example 1: Input: str1 = "*@#*" str2 = "*#" Output: 2 Explanation: str1 = "*@#*" and str2 = "*#" Two bridges can be built between the banks of the river in the following manner. * @ # * | | * # Example 2: Input: str1 = "***" str2 = "##" Output: 0 Your Task: You don't need to read input or print anything. Complete the function build_bridges() that takes str1 and str2 as input parameters and returns the maximum number of bridges that can be built. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(N*M) Constraints: 1 ≤ N, M ≤ 100 Where, N and M are the size of the string str1 and str2 respectively.
class Solution: def build_bridges(self, str1, str2): memo = [[(0) for _ in range(len(str2) + 1)] for _ in range(len(str1) + 1)] for i in range(len(str1)): for j in range(len(str2)): memo[i + 1][j] = max(memo[i + 1][j], memo[i][j]) memo[i][j + 1] = max(memo[i][j + 1], memo[i][j]) if str1[i] == str2[j]: memo[i + 1][j + 1] = max(memo[i + 1][j + 1], memo[i][j] + 1) return max(max(memo[-1]), max(r[-1] for r in memo))
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR
Penelope and her classmates are lost in the Forbidden Forest and the Devil is out to get them. But Penelope has magical powers that can build bridges across the dangerous river and take her friends to safety. The only bridges that can withstand the Devil's wrath are the ones built between two similar trees in the forest. Given str1 and str2 denoting the order of trees on either side of the river, find the maximum number of bridges that Penelope can build and save everyone from the Devil. Note: Each tree in the forest belongs to one of the 3 categories represented by * or # or @, and it's easy to understand that bridges do not intersect or cross each other. Example 1: Input: str1 = "*@#*" str2 = "*#" Output: 2 Explanation: str1 = "*@#*" and str2 = "*#" Two bridges can be built between the banks of the river in the following manner. * @ # * | | * # Example 2: Input: str1 = "***" str2 = "##" Output: 0 Your Task: You don't need to read input or print anything. Complete the function build_bridges() that takes str1 and str2 as input parameters and returns the maximum number of bridges that can be built. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(N*M) Constraints: 1 ≤ N, M ≤ 100 Where, N and M are the size of the string str1 and str2 respectively.
class Solution: def build_bridges(self, str1, str2): dp = [[(0) for i in range(len(str2) + 1)] for j in range(len(str1) + 1)] for i in range(len(str1) + 1): for j in range(len(str2) + 1): if i == 0 or j == 0: dp[i][j] = 0 elif str1[i - 1] == str2[j - 1]: dp[i][j] = 1 + dp[i - 1][j - 1] else: dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) return dp[len(str1)][len(str2)]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Penelope and her classmates are lost in the Forbidden Forest and the Devil is out to get them. But Penelope has magical powers that can build bridges across the dangerous river and take her friends to safety. The only bridges that can withstand the Devil's wrath are the ones built between two similar trees in the forest. Given str1 and str2 denoting the order of trees on either side of the river, find the maximum number of bridges that Penelope can build and save everyone from the Devil. Note: Each tree in the forest belongs to one of the 3 categories represented by * or # or @, and it's easy to understand that bridges do not intersect or cross each other. Example 1: Input: str1 = "*@#*" str2 = "*#" Output: 2 Explanation: str1 = "*@#*" and str2 = "*#" Two bridges can be built between the banks of the river in the following manner. * @ # * | | * # Example 2: Input: str1 = "***" str2 = "##" Output: 0 Your Task: You don't need to read input or print anything. Complete the function build_bridges() that takes str1 and str2 as input parameters and returns the maximum number of bridges that can be built. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(N*M) Constraints: 1 ≤ N, M ≤ 100 Where, N and M are the size of the string str1 and str2 respectively.
def f(A, B, i, j, N, M, dp): if i == N or j == M: return 0 if dp[i][j] != -1: return dp[i][j] ans = 0 if A[i] == B[j]: ans = 1 + f(A, B, i + 1, j + 1, N, M, dp) else: ans = max(f(A, B, i + 1, j, N, M, dp), f(A, B, i, j + 1, N, M, dp)) dp[i][j] = ans return ans class Solution: def build_bridges(self, A, B): N = len(A) M = len(B) dp = [[(-1) for i in range(M)] for i in range(N)] ans = f(A, B, 0, 0, N, M, dp) return ans
FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR VAR RETURN VAR
Penelope and her classmates are lost in the Forbidden Forest and the Devil is out to get them. But Penelope has magical powers that can build bridges across the dangerous river and take her friends to safety. The only bridges that can withstand the Devil's wrath are the ones built between two similar trees in the forest. Given str1 and str2 denoting the order of trees on either side of the river, find the maximum number of bridges that Penelope can build and save everyone from the Devil. Note: Each tree in the forest belongs to one of the 3 categories represented by * or # or @, and it's easy to understand that bridges do not intersect or cross each other. Example 1: Input: str1 = "*@#*" str2 = "*#" Output: 2 Explanation: str1 = "*@#*" and str2 = "*#" Two bridges can be built between the banks of the river in the following manner. * @ # * | | * # Example 2: Input: str1 = "***" str2 = "##" Output: 0 Your Task: You don't need to read input or print anything. Complete the function build_bridges() that takes str1 and str2 as input parameters and returns the maximum number of bridges that can be built. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(N*M) Constraints: 1 ≤ N, M ≤ 100 Where, N and M are the size of the string str1 and str2 respectively.
class Solution: def build_bridges(self, text1, text2): matrix = [[(0) for j in range(len(text2) + 1)] for i in range(len(text1) + 1)] for i in range(len(text1) - 1, -1, -1): for j in range(len(text2) - 1, -1, -1): if text1[i] == text2[j]: matrix[i][j] = 1 + matrix[i + 1][j + 1] else: matrix[i][j] = max(matrix[i + 1][j], matrix[i][j + 1]) return matrix[0][0]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER NUMBER
Penelope and her classmates are lost in the Forbidden Forest and the Devil is out to get them. But Penelope has magical powers that can build bridges across the dangerous river and take her friends to safety. The only bridges that can withstand the Devil's wrath are the ones built between two similar trees in the forest. Given str1 and str2 denoting the order of trees on either side of the river, find the maximum number of bridges that Penelope can build and save everyone from the Devil. Note: Each tree in the forest belongs to one of the 3 categories represented by * or # or @, and it's easy to understand that bridges do not intersect or cross each other. Example 1: Input: str1 = "*@#*" str2 = "*#" Output: 2 Explanation: str1 = "*@#*" and str2 = "*#" Two bridges can be built between the banks of the river in the following manner. * @ # * | | * # Example 2: Input: str1 = "***" str2 = "##" Output: 0 Your Task: You don't need to read input or print anything. Complete the function build_bridges() that takes str1 and str2 as input parameters and returns the maximum number of bridges that can be built. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(N*M) Constraints: 1 ≤ N, M ≤ 100 Where, N and M are the size of the string str1 and str2 respectively.
class Solution: def build_bridges(self, str1, str2): n1 = len(str1) n2 = len(str2) i = 1 j = 1 arr = [[(0) for j in range(n2 + 1)] for i in range(n1 + 1)] while i <= n1: j = 1 while j <= n2: if str1[i - 1] == str2[j - 1]: arr[i][j] = 1 + arr[i - 1][j - 1] else: arr[i][j] = max(arr[i - 1][j], arr[i][j - 1]) j += 1 i += 1 return arr[n1][n2]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR VAR
Penelope and her classmates are lost in the Forbidden Forest and the Devil is out to get them. But Penelope has magical powers that can build bridges across the dangerous river and take her friends to safety. The only bridges that can withstand the Devil's wrath are the ones built between two similar trees in the forest. Given str1 and str2 denoting the order of trees on either side of the river, find the maximum number of bridges that Penelope can build and save everyone from the Devil. Note: Each tree in the forest belongs to one of the 3 categories represented by * or # or @, and it's easy to understand that bridges do not intersect or cross each other. Example 1: Input: str1 = "*@#*" str2 = "*#" Output: 2 Explanation: str1 = "*@#*" and str2 = "*#" Two bridges can be built between the banks of the river in the following manner. * @ # * | | * # Example 2: Input: str1 = "***" str2 = "##" Output: 0 Your Task: You don't need to read input or print anything. Complete the function build_bridges() that takes str1 and str2 as input parameters and returns the maximum number of bridges that can be built. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(N*M) Constraints: 1 ≤ N, M ≤ 100 Where, N and M are the size of the string str1 and str2 respectively.
class Solution: def build_bridges(self, str1, str2): dp = [[(-1) for x in range(len(str1) + 1)] for y in range(len(str2) + 1)] for x in range(len(dp) - 1, -1, -1): for y in range(len(dp[x]) - 1, -1, -1): if x == len(dp) - 1 or y == len(dp[x]) - 1: dp[x][y] = 0 elif str1[y] == str2[x]: dp[x][y] = dp[x + 1][y + 1] + 1 else: dp[x][y] = max(dp[x + 1][y], dp[x][y + 1]) return dp[0][0] if __name__ == "__main__": T = int(input()) for _ in range(T): obj = Solution() str1, str2 = input().split() print(obj.build_bridges(str1, str2))
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def countStrings(self, n): EZ = [0] * (n + 1) E0 = [0] * (n + 1) EZ[0] = 0 EZ[1] = 1 E0[0] = 0 E0[1] = 1 for i in range(2, n + 1): EZ[i] = (EZ[i - 1] + E0[i - 1]) % 1000000007 E0[i] = EZ[i - 1] % 1000000007 res = (EZ[n] + E0[n]) % 1000000007 return res
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def countStrings(self, n): a = 1 b = 1 i = 1 while i < n: temp = a + b a = b b = temp i += 1 return (a + b) % 1000000007
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def countStrings(self, n): dp0 = [0, 1] dp1 = [0, 1] for i in range(2, n + 1): dp0.append(dp1[i - 1]) dp1.append((dp0[i - 1] + dp1[i - 1]) % 1000000007) return (dp0[n] + dp1[n]) % 1000000007
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR VAR NUMBER
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def countStrings(self, n): dp = [0] * (n + 1) dp1 = [1] * (n + 1) dp[0] = 1 dp[1] = 1 for i in range(2, n + 1): dp[i] = dp[i - 1] + dp1[i - 1] dp1[i] = dp[i - 1] s = dp[-1] + dp1[-1] return s % (10**9 + 7)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
import sys sys.setrecursionlimit(10000000) class Solution: cache = dict() def countStrings(self, n): if n == 0: return 1 if n == 1: return 2 if self.cache.get(n): return self.cache.get(n) value = self.countStrings(n - 2) + self.countStrings(n - 1) value = value % (10**9 + 7) if self.cache.get(n) is None: self.cache[n] = value return value
IMPORT EXPR FUNC_CALL VAR NUMBER CLASS_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR VAR RETURN VAR
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def countStrings(self, n): oc1 = 1 oc0 = 1 for i in range(2, n + 1): nc1 = oc0 nc0 = oc0 + oc1 oc0 = nc0 oc1 = nc1 return (oc0 + oc1) % (10**9 + 7)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def countStrings(self, n): MOD = 10**9 + 7 dp = [0] * n dp2 = [0] * n dp[0] = 1 dp2[0] = 1 for i in range(1, n): dp[i] = (dp[i - 1] + dp2[i - 1]) % MOD dp2[i] = dp[i - 1] % MOD return (dp[n - 1] + dp2[n - 1]) % MOD
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def countStrings(self, n): dp = [(0) for i in range(n + 1)] dp[0] = 2 dp[1] = 3 i = 2 while i < n: dp[i] = (dp[i - 2] + dp[i - 1]) % 1000000007 i += 1 return dp[n - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR BIN_OP VAR NUMBER
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def countStrings(self, n): a = [] b = [] a.append(1) b.append(1) for i in range(1, n): temp1 = a[i - 1] + b[i - 1] a.append(temp1) temp2 = a[i - 1] b.append(temp2) return (a[n - 1] + b[n - 1]) % 1000000007
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def countStrings(self, n): endl = [0] * (n + 1) endz = [0] * (n + 1) endl[0] = endz[0] = 0 endl[1] = endz[1] = 1 for i in range(2, n + 1): endl[i] = endz[i - 1] endz[i] = endl[i - 1] + endz[i - 1] return (endl[-1] + endz[-1]) % (10**9 + 7)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def countStrings(self, n): if n == 0: return 0 if n == 1: return 2 eo = [0] * n ea = [0] * n ea[1] = 1 eo[1] = 1 for i in range(2, n): eo[i] = eo[i - 1] + ea[i - 1] ea[i] = eo[i - 1] eo.append(eo[-1] + ea[-1]) ea.append(eo[-2]) return (eo[-1] + ea[-1]) % 1000000007
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
mod = 10**9 + 7 class Solution: def countStrings(self, n): sub_str_ending_zero = 1 sub_str_ending_one = 1 for i in range(1, n): tmp = (sub_str_ending_zero + sub_str_ending_one) % mod sub_str_ending_one = sub_str_ending_zero sub_str_ending_zero = tmp return (sub_str_ending_zero + sub_str_ending_one) % mod
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def countStrings(self, n): end_1 = [0, 1] end_0 = [0, 1] if n == 0: return 0 elif n == 1: return 2 for i in range(2, n + 1): end_1.append(end_0[i - 1]) end_0.append(end_1[i - 1] + end_0[i - 1]) b = end_0[n] + end_1[n] return b % (pow(10, 9) + 7)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def countStrings(self, n): mod = 10**9 + 7 l1 = [(0) for i in range(n)] l2 = [(0) for i in range(n)] l1[0] = 1 l2[0] = 1 for i in range(1, n): l1[i] = l1[i - 1] + l2[i - 1] l2[i] = l1[i - 1] return (l1[-1] + l2[-1]) % mod
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def countStrings(self, n): dp = {} mod = 10**9 + 7 dp = [{} for _ in range(n)] dp[-1][0] = 1 dp[-1][1] = 1 for i in range(n - 2, -1, -1): for prev in dp[i + 1]: if prev == 0: dp[i][0] = dp[i + 1][0] dp[i][1] = dp[i + 1][0] else: dp[i][0] += dp[i + 1][1] total = 0 total += dp[0][0] total += dp[0][1] return total % (10**9 + 7)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR DICT VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
import sys sys.setrecursionlimit(10**7) class Solution: def countStrings(self, n): g = {} def recr(val, index): if (val, index) in g: return g[val, index] if index == n: return 1 if val == "": ans = recr(0, index + 1) + recr(1, index + 1) elif val == 0: ans = recr(1, index + 1) + recr(0, index + 1) elif val == 1: ans = recr(0, index + 1) g[val, index] = ans return ans return recr("", 0) % (10**9 + 7)
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR RETURN BIN_OP FUNC_CALL VAR STRING NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def countStrings(self, n): a = [(0) for i in range(n)] b = [(0) for i in range(n)] mod = 10**9 + 7 a[0] = 1 b[0] = 1 for i in range(1, n): a[i] = (a[i - 1] + b[i - 1]) % mod b[i] = a[i - 1] % mod return (a[n - 1] + b[n - 1]) % mod
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def countStrings(self, n): dp = [([0] * 2) for i in range(n)] dp[0][0] = 1 dp[0][1] = 1 for i in range(1, n): dp[i][0] = (dp[i - 1][0] + dp[i - 1][1]) % 1000000007 dp[i][1] = dp[i - 1][0] return sum(dp[n - 1]) % 1000000007
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def countStrings(self, n): if n == 0: return 0 elif n == 1: return 2 one = 1 zero = 1 for i in range(2, n + 1): temp = zero zero = one + zero one = temp temp = one + zero mod = 10**9 + 7 return temp % mod
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP VAR VAR
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def countStrings(self, n): if n < 3: return n + 1 l = [0] * (n + 1) l[0] = 1 l[1] = 2 l[2] = 3 for i in range(3, n + 1): l[i] = (l[i - 1] + l[i - 2]) % 1000000007 return l[n]
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def countStrings(self, n): out = [2, 3] for i in range(n - 2): out.append(out[-1] + out[-2]) return out[n - 1] % 1000000007
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR NUMBER NUMBER
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def countStrings(self, n): dp = [(0) for i in range(n + 1)] z = 1 o = 0 m = 10**9 + 7 res = 0 for i in range(1, n): z, o = (o + z) % m, z res = (2 * z + o) % m return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR RETURN VAR
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def countStrings(self, n): way = [1, 1] for x in range(n - 1): way[0], way[1] = way[0] + way[1], way[0] return sum(way) % (10**9 + 7)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def countStrings(self, n): if n == 1: return 2 if n == 2: return 3 dp = [-1] * n dp[0] = 2 dp[1] = 3 for i in range(2, n): dp[i] = dp[i - 2] + dp[i - 1] return dp[n - 1] % (10**9 + 7)
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def countStrings(self, n): if n <= 2: return n + 1 a = 2 b = 3 for i in range(3, n + 1): c = (a + b) % (10**9 + 7) a = b b = c return c
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def countStrings(self, n): zero = 1 one = 1 mod = 10**9 + 7 ans = zero + one if n == 1: return ans i = 2 while i <= n: one = zero % mod zero = ans % mod ans = (one + zero) % mod i += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def countStrings(self, n): dp = [[(0) for _ in range(n + 1)] for _ in range(2)] dp[0][1] = 1 dp[1][1] = 1 for i in range(2, len(dp[0])): dp[1][i] = dp[0][i - 1] dp[0][i] = dp[0][i - 1] + dp[1][i - 1] return (dp[0][-1] + dp[1][-1]) % (10**9 + 7)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def countStrings(self, n): MOD = 10**9 + 7 if n == 1: return 2 if n == 2: return 3 count1 = 2 count2 = 3 for i in range(3, n + 1): nextcount = (count1 + count2) % MOD count1, count2 = count2, nextcount return nextcount
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def countStrings(self, n): mod = 10**9 + 7 l = [(0) for i in range(n + 1)] l[0] = 1 l[1] = 2 for i in range(2, n + 1): l[i] = l[i - 1] + l[i - 2] return l[-1] % mod
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER VAR
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def countStrings(self, n): def countBinaryStrings(n): MOD = 10**9 + 7 count0 = 1 count1 = 1 for i in range(2, n + 1): new_count0 = (count0 + count1) % MOD new_count1 = count0 count0 = new_count0 count1 = new_count1 return (count0 + count1) % MOD return countBinaryStrings(n)
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR VAR
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def countStrings(self, n): countB = 1 countS = 1 for _ in range(2, n + 1): prev_countB = countB prev_countS = countS countS = prev_countB + prev_countS countB = prev_countS result = countS + countB return result % 1000000007
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP VAR NUMBER
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
class Solution: def countStrings(self, n): a = 0 b = 1 c = 1 for i in range(1, n + 2): c = (a + b) % 1000000007 a = b b = c return c % 1000000007
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR NUMBER
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Output your answer modulo 10^9 + 7. Example 1: Input: N = 3 Output: 5 Explanation: 5 strings are (000, 001, 010, 100, 101). Example 2: Input: N = 2 Output: 3 Explanation: 3 strings are (00,01,10). Your Task: Complete the function countStrings() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5}
mod = 10**9 + 7 def multiply(mat1, mat2): ans = [[(0) for i in range(2)] for j in range(2)] for i in range(2): for j in range(2): for k in range(2): ans[i][j] += mat1[i][k] * mat2[k][j] % mod return ans def func(mat, n): if n == 1: return mat temp = func(mat, n // 2) if n % 2 == 0: return multiply(temp, temp) return multiply(multiply(temp, temp), mat) class Solution: def countStrings(self, N): mat = [[1, 1], [1, 0]] ans = func(mat, N + 2) return ans[0][1] % mod
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER NUMBER VAR