Skip to content
🤔prompts chat🧠
🔍
question:class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def add_trees_in_reverse_level_order(root1, root2): Returns the sum of two binary trees represented as numbers in reversed level-order format. pass def create_tree_from_list(vals): if not vals: return None root = TreeNode(vals[0]) queue = [root] i = 1 while i < len(vals): current = queue.pop(0) if i < len(vals) and vals[i] is not None: current.left = TreeNode(vals[i]) queue.append(current.left) i += 1 if i < len(vals) and vals[i] is not None: current.right = TreeNode(vals[i]) queue.append(current.right) i += 1 return root def tree_to_list(root): result = [] current = root while current: result.append(current.val) current = current.left return result def test_add_trees_in_reverse_level_order_simple(): root1 = create_tree_from_list([2, 4, 3]) root2 = create_tree_from_list([5, 6, 4]) result_tree = add_trees_in_reverse_level_order(root1, root2) assert tree_to_list(result_tree) == [7, 0, 8] def test_add_trees_in_reverse_level_order_with_carry(): root1 = create_tree_from_list([9, 9, 9]) root2 = create_tree_from_list([1, 1, 1]) result_tree = add_trees_in_reverse_level_order(root1, root2) assert tree_to_list(result_tree) == [0, 1, 1, 1] def test_add_trees_in_reverse_level_order_different_lengths(): root1 = create_tree_from_list([1, 0, 0, 0, 0]) root2 = create_tree_from_list([1]) result_tree = add_trees_in_reverse_level_order(root1, root2) assert tree_to_list(result_tree) == [2, 0, 0, 0, 0] def test_add_trees_in_reverse_level_order_with_no_carry(): root1 = create_tree_from_list([1, 2, 3]) root2 = create_tree_from_list([4, 5, 6]) result_tree = add_trees_in_reverse_level_order(root1, root2) assert tree_to_list(result_tree) == [5, 7, 9] def test_add_trees_in_reverse_level_order_with_zeros(): root1 = create_tree_from_list([0]) root2 = create_tree_from_list([0]) result_tree = add_trees_in_reverse_level_order(root1, root2) assert tree_to_list(result_tree) == [0]

answer:class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def add_trees_in_reverse_level_order(root1, root2): Returns the sum of two binary trees represented as numbers in reversed level-order format. def add_lists_as_numbers(l1, l2): carry, head = 0, None current = None while l1 or l2 or carry: val1 = l1.pop(0) if l1 else 0 val2 = l2.pop(0) if l2 else 0 total = val1 + val2 + carry carry = total // 10 new_node = TreeNode(total % 10) if not head: head = new_node current = head else: current.left = new_node current = current.left return head def tree_to_list(tree): q, result = [tree], [] while q: node = q.pop(0) if node: result.append(node.val) q.append(node.left) q.append(node.right) return result list1 = tree_to_list(root1) list2 = tree_to_list(root2) return add_lists_as_numbers(list1, list2)

question:def find_subarray_with_target_sum(nums: List[int], target: int) -> List[int]: Find a contiguous subarray whose sum is exactly equal to the target. >>> find_subarray_with_target_sum([1, 2, 3, 4, 5], 9) == [2, 3, 4] >>> find_subarray_with_target_sum([1, -1, 5, -2, 3], 3) == [1, -1, 5, -2] >>> find_subarray_with_target_sum([1, 2, 3, 4, 5], 15) == [1, 2, 3, 4, 5] >>> find_subarray_with_target_sum([1, 2, 3, 4, 5], 8) == []

answer:def find_subarray_with_target_sum(nums, target): Find a contiguous subarray whose sum is exactly equal to the target. # Dictionary to store cumulative sum and corresponding index cum_sum_map = {} current_sum = 0 for i, num in enumerate(nums): # Add the current number to the running sum current_sum += num # Check if the current running sum is equal to the target if current_sum == target: return nums[:i+1] # Check if any subarray sums up to the target by checking if # (current_sum - target) is present in the cumulative sum map if (current_sum - target) in cum_sum_map: return nums[cum_sum_map[current_sum - target] + 1:i + 1] # Store the current running sum with its index in the map cum_sum_map[current_sum] = i return []

question:from typing import List def numIslands(mat: List[List[int]]) -> int: Returns the number of islands in the given binary matrix. An island is a group of connected 1's surrounded by 0's. >>> numIslands([[1, 1, 0, 0, 0], [1, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 1]]) 3 >>> numIslands([[0, 0, 0], [0, 0, 0], [0, 0, 0]]) 0 >>> numIslands([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) 1 >>> numIslands([]) 0 >>> numIslands([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) 4 >>> numIslands([[1, 0, 1, 0], [0, 1, 0, 1], [1, 0, 1, 0], [0, 1, 0, 1]]) 8

answer:from typing import List def numIslands(mat: List[List[int]]) -> int: Returns the number of islands in the given binary matrix. An island is a group of connected 1's surrounded by 0's. if not mat: return 0 m, n = len(mat), len(mat[0]) visited = [[False] * n for _ in range(m)] def dfs(i, j): if i < 0 or i >= m or j < 0 or j >= n or mat[i][j] == 0 or visited[i][j]: return visited[i][j] = True dfs(i+1, j) # down dfs(i-1, j) # up dfs(i, j+1) # right dfs(i, j-1) # left count = 0 for i in range(m): for j in range(n): if mat[i][j] == 1 and not visited[i][j]: dfs(i, j) count += 1 return count

question:def longest_valid_parentheses(s: str) -> int: Finds the length of the longest valid parentheses substring. Parameters: s (str): A string consisting of '(' and ')'. Returns: int: The length of the longest valid parentheses substring. >>> longest_valid_parentheses("()()") 4 >>> longest_valid_parentheses("((()))") 6 >>> longest_valid_parentheses(")()())") 4 >>> longest_valid_parentheses("(") 0 >>> longest_valid_parentheses("") 0

answer:def longest_valid_parentheses(s): Finds the length of the longest valid parentheses substring. Parameters: s (str): A string consisting of '(' and ')'. Returns: int: The length of the longest valid parentheses substring. max_len = 0 stack = [] start = -1 for i, char in enumerate(s): if char == '(': stack.append(i) else: if stack: stack.pop() if stack: max_len = max(max_len, i - stack[-1]) else: max_len = max(max_len, i - start) else: start = i return max_len

Released under the chat License.

has loaded