Appearance
question:class RangeSumDict: A dictionary-like data structure that supports the efficient addition of a key-value pair and the range_sum operation. def __init__(self): # Initialize your data structure here pass def add(self, key, value): Add a key-value pair to the data structure. Args: key (int): The key to add. value (int): The value associated with the key. pass def range_sum(self, low, high): Return the sum of all values with keys in the range [low, high]. Args: low (int): The lower bound of the range. high (int): The upper bound of the range. Returns: int: The sum of all values with keys in the range. pass def process_commands(commands): Process a list of commands and return the results of range_sum operations. Args: commands (List[str]): The list of commands to process. Returns: List[int]: The results of the range_sum operations. >>> process_commands([ "add 1 100", "add 3 200", "add 2 50", "range_sum 1 3", "add 4 10", "range_sum 2 4", "add 2 150", "range_sum 2 2", "range_sum 3 5" ]) [350, 260, 200, 210] pass
answer:class RangeSumDict: def __init__(self): self.data = {} def add(self, key, value): self.data[key] = self.data.get(key, 0) + value def range_sum(self, low, high): total = 0 for key in range(low, high + 1): total += self.data.get(key, 0) return total def process_commands(commands): rsd = RangeSumDict() output = [] for command in commands: parts = command.split() if parts[0] == "add": key = int(parts[1]) value = int(parts[2]) rsd.add(key, value) elif parts[0] == "range_sum": low = int(parts[1]) high = int(parts[2]) output.append(rsd.range_sum(low, high)) return output
question:def minimum_outposts(n: int, m: int, roads: List[Tuple[int, int]]) -> int: The kingdom of Linaria has n villages connected by m roads. Some of these roads are known to be dangerous due to bandit activity. The goal is to place outposts such that every road's two villages can communicate through guards directly or indirectly via these outposts. The function should return the minimum number of outposts required to fulfill this requirement. Args: n (int): The number of villages. m (int): The number of roads. roads (List[Tuple[int, int]]): The list of roads represented by tuples of connected villages. Returns: int: The minimum number of outposts required. Example: >>> minimum_outposts(5, 4, [(1, 2), (2, 3), (3, 4), (4, 5)]) 1 >>> minimum_outposts(4, 2, [(1, 2), (3, 4)]) 2
answer:def find_connected_components(n, edges): from collections import defaultdict, deque def bfs(start, visited, graph): queue = deque([start]) while queue: node = queue.popleft() for neighbor in graph[node]: if not visited[neighbor]: visited[neighbor] = True queue.append(neighbor) graph = defaultdict(list) for u, v in edges: graph[u].append(v) graph[v].append(u) visited = [False] * (n + 1) connected_components = 0 for i in range(1, n + 1): if not visited[i]: connected_components += 1 visited[i] = True bfs(i, visited, graph) return connected_components def minimum_outposts(n, m, roads): return find_connected_components(n, roads)
question:def minimal_number_of_players(match_results: List[Tuple[int, int]]) -> int: Given a list of match results, determine the minimal number of players who could have played in the tournament. Args: match_results (List[Tuple[int, int]]): List of tuples with each tuple containing two integers u and v where u is the winner and v is the loser. Returns: int: The minimum number of players who could have participated in the tournament. Examples: >>> minimal_number_of_players([(1, 2), (2, 3), (1, 3)]) 3 >>> minimal_number_of_players([(4, 5), (6, 7), (5, 6), (7, 8), (4, 8)]) 5
answer:def minimal_number_of_players(match_results): players = set() for winner, loser in match_results: players.add(winner) players.add(loser) return len(players)
question:def max_friends_served(n, m, k, favorite_flavors): Determine the maximum number of friends who can definitely get their favorite ice cream flavor. Parameters: n (int): The number of different flavors. m (int): The number of friends. k (int): The number of scoops initially available for each flavor. favorite_flavors (list): The favorite flavor of each friend. Returns: int: The maximum number of friends who can definitely get their favorite ice cream flavor. >>> max_friends_served(5, 3, 2, [1, 2, 2]) 3 >>> max_friends_served(4, 4, 1, [1, 2, 1, 3]) 3
answer:def max_friends_served(n, m, k, favorite_flavors): Determine the maximum number of friends who can definitely get their favorite ice cream flavor. Parameters: n (int): The number of different flavors. m (int): The number of friends. k (int): The number of scoops initially available for each flavor. favorite_flavors (list): The favorite flavor of each friend. Returns: int: The maximum number of friends who can definitely get their favorite ice cream flavor. from collections import Counter flavor_counts = Counter(favorite_flavors) max_served = 0 for flavor, count in flavor_counts.items(): max_served += min(count, k) return max_served