Appearance
question:def transform_durations(n, durations): Transforms the list so that any duration that exceeds 60 seconds is replaced by 60 seconds. Parameters: n (int): Number of durations durations (List[int]): List of message durations Returns: List[int]: Transformed list of durations >>> transform_durations(5, [45, 67, 120, 30, 90]) [45, 60, 60, 30, 60] >>> transform_durations(3, [15, 10, 5]) [15, 10, 5]
answer:def transform_durations(n, durations): Transforms the list so that any duration that exceeds 60 seconds is replaced by 60 seconds. Parameters: n (int): Number of durations durations (List[int]): List of message durations Returns: List[int]: Transformed list of durations return [min(duration, 60) for duration in durations]
question:def find_min_street_lamps(n: int, roads: List[Tuple[int, int, int]], max_time: int) -> int: Determine the minimum number of street lamps required so that every intersection in the city is adequately lit. >>> find_min_street_lamps(4, [(1, 2, 2), (2, 3, 4), (3, 4, 1), (4, 1, 3)], 3) == 2 >>> find_min_street_lamps(1, [], 1) == 1 >>> find_min_street_lamps(5, [(1, 2, 1), (1, 3, 1), (1, 4, 1), (1, 5, 1), (2, 3, 1), (2, 4, 1), (2, 5, 1), (3, 4, 1), (3, 5, 1), (4, 5, 1)], 1) == 1 >>> find_min_street_lamps(6, [(1, 2, 1), (3, 4, 1), (5, 6, 1)], 1) == 3
answer:import heapq def dijkstra(n, graph, start, max_time): distances = [float('inf')] * n distances[start] = 0 min_heap = [(0, start)] while min_heap: current_distance, u = heapq.heappop(min_heap) if current_distance > distances[u]: continue for v, weight in graph[u]: distance = current_distance + weight if distance < distances[v]: distances[v] = distance heapq.heappush(min_heap, (distance, v)) return [i for i in range(n) if distances[i] <= max_time] def find_min_street_lamps(n, roads, max_time): graph = [[] for _ in range(n)] for u, v, w in roads: graph[u-1].append((v-1, w)) graph[v-1].append((u-1, w)) all_intersections = set(range(n)) street_lamps = 0 while all_intersections: u = all_intersections.pop() reachable = set(dijkstra(n, graph, u, max_time)) all_intersections -= reachable street_lamps += 1 return street_lamps def solution(): import sys input = sys.stdin.read data = input().split() n = int(data[0]) m = int(data[1]) roads = [] index = 2 for _ in range(m): u, v, w = map(int, (data[index], data[index+1], data[index+2])) roads.append((u, v, w)) index += 3 max_time = int(data[index]) print(find_min_street_lamps(n, roads, max_time))
question:def is_valid_parentheses(s: str) -> bool: Determine if the input string containing only '(' and ')' is a valid parentheses string. >>> is_valid_parentheses("()") == True >>> is_valid_parentheses("(())") == True >>> is_valid_parentheses("((()()))") == True >>> is_valid_parentheses("(((((())))))") == True
answer:def is_valid_parentheses(s): Determine if the input string containing only '(' and ')' is a valid parentheses string. Args: s (str): Input string containing only '(' and ')'. Returns: bool: True if the string is a valid parentheses string, False otherwise. stack = [] for char in s: if char == '(': stack.append(char) elif char == ')': if not stack: return False stack.pop() return not stack # The stack should be empty if the string is valid
question:from typing import List def largest_identical_square(matrix: List[List[int]]) -> int: Implement a function that, given a matrix of integers, returns the size of the largest square submatrix which contains all identical elements. >>> largest_identical_square([[1]]) == 1 >>> largest_identical_square([ ... [2, 2, 3, 4], ... [5, 2, 2, 2], ... [9, 2, 2, 2], ... [7, 7, 7, 7] ... ]) == 2 >>> largest_identical_square([ ... [1, 2], ... [3, 4] ... ]) == 1 >>> largest_identical_square([ ... [2, 2, 2], ... [2, 2, 2], ... [2, 2, 2] ... ]) == 3 >>> largest_identical_square([[1]*500 for _ in range(500)]) == 500
answer:from typing import List def largest_identical_square(matrix: List[List[int]]) -> int: if not matrix or not matrix[0]: return 0 nrows = len(matrix) ncols = len(matrix[0]) dp = [[0] * ncols for _ in range(nrows)] max_square_size = 1 for i in range(nrows): for j in range(ncols): if i == 0 or j == 0: dp[i][j] = 1 else: if matrix[i][j] == matrix[i-1][j] and matrix[i][j] == matrix[i][j-1] and matrix[i][j] == matrix[i-1][j-1]: dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1 else: dp[i][j] = 1 max_square_size = max(max_square_size, dp[i][j]) return max_square_size