Appearance
question:def subarray_sum(nums: List[int], target: int) -> bool: Determines if a continuous subarray sums to target. >>> subarray_sum([1, 2, 3, 4], 6) True >>> subarray_sum([1, 2, 3, 4], 10) True >>> subarray_sum([], 0) False >>> subarray_sum([1], 1) True >>> subarray_sum([1], -1) False >>> subarray_sum([0], 0) True >>> subarray_sum([10**6, -10**6, 1, 2, 3], 5) True >>> subarray_sum([1, 2, 3, -10**6, 10**6], 3) True >>> subarray_sum([-1, -2, -3, 1, 2, 3], 0) True >>> subarray_sum([-1, -1, -1, -1], -3) True >>> subarray_sum([1, 2, 3, 4, 5], 20) False >>> subarray_sum([-1, -2, -3, -4, -5], 1) False
answer:def subarray_sum(nums, target): Determines if a continuous subarray sums to target. Args: nums : list of int - The input list of integers. target : int - The target integer sum. Returns: bool - True if such a subarray exists, False otherwise. current_sum = 0 sum_map = {0: -1} # Maps cumulative sum to index for quick lookup for index, num in enumerate(nums): current_sum += num if (current_sum - target) in sum_map: return True sum_map[current_sum] = index return False
question:def smallest_possible_length(s: str) -> int: Determines the length of the smallest possible string that can be obtained after applying deletions on adjacent '01' or '10' pairs any number of times. Parameters: s (str): Input string containing only the characters '0' and '1'. Returns: int: The length of the final string after performing all possible deletions. Examples: >>> smallest_possible_length("1100") 0 >>> smallest_possible_length("1001") 0 >>> smallest_possible_length("1010") 0 >>> smallest_possible_length("1111") 4 >>> smallest_possible_length("0000") 4 >>> smallest_possible_length("1101001") 1 >>> smallest_possible_length("01010") 1 >>> smallest_possible_length("1010101") 1 >>> smallest_possible_length("0101") 0 >>> smallest_possible_length("10101") 1 >>> smallest_possible_length("0") 1 >>> smallest_possible_length("1") 1
answer:def smallest_possible_length(s): Determines the length of the smallest possible string that can be obtained after applying deletions on adjacent '01' or '10' pairs any number of times. Parameters: s (str): Input string containing only the characters '0' and '1'. Returns: int: The length of the final string after performing all possible deletions. stack = [] for char in s: if stack and stack[-1] != char: stack.pop() else: stack.append(char) return len(stack)
question:def min_time(tasks): Returns the minimum possible time required to finish all tasks using two processors. tasks: List of integers where tasks[i] represents the amount of time it takes to complete the ith task. Return an integer representing the minimum possible time required to finish all tasks.
answer:def min_time(tasks): Returns the minimum possible time required to finish all tasks using two processors. tasks: List of integers where tasks[i] represents the amount of time it takes to complete the ith task. Return an integer representing the minimum possible time required to finish all tasks. tasks = sorted(tasks, reverse=True) # Sort tasks in descending order processor_1 = 0 processor_2 = 0 for task in tasks: if processor_1 <= processor_2: processor_1 += task else: processor_2 += task return max(processor_1, processor_2)
question:def min_removals_to_equalize_grid(grid: List[List[int]]) -> int: Given an m x n integer grid `grid`, return the minimum number of cells you need to remove to make all the remaining elements in the grid equal. You can remove a cell from the grid only if it has at least one immediate neighbor (top, bottom, left, or right). If it is impossible to make all remaining elements equal, return `-1`. >>> min_removals_to_equalize_grid([[1, 2, 1], [1, 1, 2], [2, 1, 1]]) == 3 >>> min_removals_to_equalize_grid([[1, 1], [2, 1]]) == 1 >>> min_removals_to_equalize_grid([[3, 3, 3], [3, 2, 3], [3, 3, 3]]) == 1 >>> min_removals_to_equalize_grid([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) == -1 >>> min_removals_to_equalize_grid([[1, 2], [3, 4]]) == -1 >>> min_removals_to_equalize_grid([[1]]) == 0 >>> min_removals_to_equalize_grid([[1, 1], [1, 1]]) == 0
answer:def min_removals_to_equalize_grid(grid): from collections import Counter import operator m, n = len(grid), len(grid[0]) # Flatten the grid into a list of elements elements = [grid[i][j] for i in range(m) for j in range(n)] # Count the frequency of each element using Counter frequency = Counter(elements) # If the most common element count covers more than the half of the cells, it’s possible to make the grid equal max_count = max(frequency.items(), key=operator.itemgetter(1))[1] if max_count >= (m * n) // 2 + 1: return (m * n) - max_count else: return -1