Appearance
question:Given a binary tree, write a function to determine the largest value in each row of the tree. The rows are numbered starting from 0, where the root node is in the 0th row. For example, consider the binary tree represented as follows: ``` 1 / 3 2 / 5 3 9 ``` Example 1: Input: [1, 3, 2, 5, 3, null, 9] Output: [1, 3, 9] Example 2: Input: [4, 2, 6, 1, null, 5, 7] Output: [4, 6, 7] Note: The number of nodes in the tree will be in the range [1, 10^4]. Each node’s value will be in the range [-10^5, 10^5].
answer:from collections import deque class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def largestValues(root): if not root: return [] result = [] queue = deque([root]) while queue: level_size = len(queue) max_value = float('-inf') for _ in range(level_size): node = queue.popleft() max_value = max(max_value, node.val) if node.left: queue.append(node.left) if node.right: queue.append(node.right) result.append(max_value) return result
question:You are given an integer array `A` consisting of `N` positive integers. Your task is to find the smallest positive integer `K` such that there exists no subset of `A` whose sum is `K`. ------ Input ------ The first line contains an integer `T`, the number of test cases. The description of `T` test cases follows. The first line of each test case contains an integer `N`, the number of elements in the array `A`. The second line of each test case contains `N` space-separated integers representing the elements of the array `A`. ------ Output ------ For each test case, output a single line containing the smallest positive integer `K` that cannot be represented as the sum of any subset of the array `A`. ------ Constraints ------ 1 ≤ T ≤ 100 1 ≤ N ≤ 10^4 1 ≤ A[i] ≤ 10^6 The sum of N over all test cases does not exceed 10^4 ------ Example ------ ----- Sample Input ------ 2 5 1 2 2 5 7 3 1 1 1 ----- Sample Output ------ 18 4 ------ Explanation ------ In the first test case, all sums from 1 to 17 can be formed using subsets of the array. But 18 cannot be formed by any subset. In the second test case, all sums from 1 to 3 can be formed using subsets of the array. But 4 cannot be formed by any subset.
answer:def find_smallest_missing_number(T, test_cases): results = [] for case in test_cases: N, A = case A.sort() smallest_missing_number = 1 for num in A: if num > smallest_missing_number: break smallest_missing_number += num results.append(smallest_missing_number) return results
question:A company requires a system that validates employee identification codes. Each ID code has specific rules that need to be checked. Your task is to write a function that validates these codes and determines if they meet the company's standards. -----Input:----- - First line will contain N, the number of employee ID codes. Then the next N lines each contains one ID code string. -----Output:----- For each ID code, output "Valid" if the code meets all the following criteria, otherwise output "Invalid". -----Criteria:----- - The ID code must be exactly 8 characters long. - It must contain at least one uppercase letter. - It must contain at least one lowercase letter. - It must contain at least one digit. - It must contain at least one special character from the set {!@#%^&*()_+}. -----Constraints----- - 1 leq N leq 100 - Each ID code string will only contain printable ASCII characters. -----Sample Input:----- 3 A1!bCdef 12345678 Abc@1234 -----Sample Output:----- Valid Invalid Valid
answer:import re def validate_id_code(id_code): Checks if the given ID code is valid based on the specified criteria. if len(id_code) != 8: return "Invalid" has_upper = re.search(r'[A-Z]', id_code) is not None has_lower = re.search(r'[a-z]', id_code) is not None has_digit = re.search(r'd', id_code) is not None has_special = re.search(r'[!@#%^&*()_+]', id_code) is not None if has_upper and has_lower and has_digit and has_special: return "Valid" return "Invalid" def validate_id_codes(codes): Given a list of ID codes, returns a list indicating which codes are valid or invalid. return [validate_id_code(code) for code in codes]
question:Vishal is trying to send a message to his friend Anika using an old mobile network that suffers from intermittent connectivity issues. The network can be represented as an array of towers, where each tower has a certain signal strength. Each tower can either be functioning or not at any given time. Vishal can send the message to Anika by finding the longest consecutive sequence of functioning towers with non-zero signal strength. However, if Vishal encounters a non-functioning tower or a tower with zero signal strength, he must stop and start over from the next tower. Write a program to determine the length of the longest consecutive sequence of functioning towers with non-zero signal strength. -----Input----- The first line contains an integer N (1 leq N leq 100000), the number of towers arranged in a linear array. The second line contains N integers, where each integer can be either -1, 0, or S (1 leq S leq 1000). A value of -1 represents a non-functioning tower, a value of 0 represents a tower with zero signal strength, and any positive integer S represents the signal strength of a functioning tower. -----Output----- Output the length of the longest consecutive sequence of functioning towers with non-zero signal strength. -----Examples----- Sample Input: 10 1 -1 3 0 1 2 3 4 -1 5 Sample Output: 4 Sample Input: 5 0 0 0 0 0 Sample Output: 0
answer:def longest_functioning_sequence(towers): max_length = 0 current_length = 0 for signal in towers: if signal > 0: current_length += 1 else: if current_length > max_length: max_length = current_length current_length = 0 if current_length > max_length: max_length = current_length return max_length