Appearance
question:def can_transform_with_one_swap(n: int, s: str, t: str) -> str: Determines if string s can be transformed into string t by performing exactly one swap operation on the characters of s. Args: n (int): The length of the strings s and t. s (str): The original string. t (str): The target string after one swap. Returns: str: "YES" if s can be transformed into t with one swap, otherwise "NO". >>> can_transform_with_one_swap(5, "abcde", "abedc") "YES" >>> can_transform_with_one_swap(3, "abc", "bca") "NO"
answer:def can_transform_with_one_swap(n, s, t): Determines if string s can be transformed into string t with exactly one swap. Args: n (int): The length of the strings s and t. s (str): The original string. t (str): The target string after one swap. Returns: str: "YES" if s can be transformed into t with one swap, otherwise "NO". if s == t: return "NO" # Find all the positions where characters differ between s and t diff_positions = [] for i in range(n): if s[i] != t[i]: diff_positions.append(i) # There must be exactly two positions where s and t differ if len(diff_positions) != 2: return "NO" # Check if swapping the two differing characters in s makes it equal to t i, j = diff_positions s_list = list(s) s_list[i], s_list[j] = s_list[j], s_list[i] return "YES" if ''.join(s_list) == t else "NO"
question:def minimize_total_waiting_time(n: int, times: List[int]) -> List[int]: Determine the order in which people should be organized in the queue to minimize the total waiting time for all people. Args: n (int): The number of people. times (List[int]): The time taken for each person to buy a ticket. Returns: List[int]: The order of people in the queue to minimize total waiting time. Examples: >>> minimize_total_waiting_time(5, [8, 1, 5, 4, 2]) [1, 2, 4, 5, 8] >>> minimize_total_waiting_time(3, [0, 0, 0]) [0, 0, 0] >>> minimize_total_waiting_time(4, [5, 5, 5, 5]) [5, 5, 5, 5] >>> minimize_total_waiting_time(6, [13, 3, 8, 18, 1, 5]) [1, 3, 5, 8, 13, 18] >>> minimize_total_waiting_time(1, [7]) [7]
answer:def minimize_total_waiting_time(n, times): Returns the order of people (based on their ticket buying times) in the queue to minimize the total waiting time. # Sort the times sorted_times = sorted(times) return sorted_times
question:def longest_increasing_subsequence(n: int, bird_counts: List[int]) -> int: Returns the length of the longest strictly increasing subsequence found in the bird_counts. If no such subsequence exists, returns -1. Parameters: n (int): Number of checkpoints bird_counts (list): List of integers representing bird counts at checkpoints Returns: int: Length of the longest strictly increasing subsequence or -1 if not found >>> longest_increasing_subsequence(7, [5, 1, 2, 3, 8, 7, 10]) == 4 >>> longest_increasing_subsequence(5, [9, 8, 7, 6, 5]) == -1
answer:def longest_increasing_subsequence(n, bird_counts): Returns the length of the longest strictly increasing subsequence found in the bird_counts. If no such subsequence exists, returns -1. Parameters: n (int): Number of checkpoints bird_counts (list): List of integers representing bird counts at checkpoints Returns: int: Length of the longest strictly increasing subsequence or -1 if not found longest_length = 0 current_length = 1 for i in range(1, n): if bird_counts[i] > bird_counts[i - 1]: current_length += 1 longest_length = max(longest_length, current_length) else: current_length = 1 return longest_length if longest_length >= 2 else -1
question:from typing import List, Tuple def find_shortest_even_path(N: int, M: int, edges: List[Tuple[int, int, int]], A: int, B: int) -> str: Find the shortest path in an undirected graph where the total path weight is even. Args: N : int : The number of vertices. M : int : The number of edges. edges : List[Tuple[int, int, int]] : The edges of the graph given as tuples (u, v, w). A : int : The start vertex. B : int : The end vertex. Returns: str : The sequence of vertices in the shortest path with an even total weight, or "-1" if such a path does not exist. >>> edges = [(1, 2, 2), (1, 3, 4), (2, 3, 1), (2, 4, 7), (3, 5, 3), (4, 5, 1)] >>> find_shortest_even_path(5, 6, edges, 1, 5) '1 2 3 5' >>> edges = [(1, 2, 1), (2, 3, 1), (3, 4, 1)] >>> find_shortest_even_path(4, 3, edges, 1, 4) '-1' >>> edges = [(1, 2, 4), (2, 3, 2), (1, 3, 6)] >>> find_shortest_even_path(3, 3, edges, 1, 3) '1 3' >>> edges = [(1, 2, 2), (1, 3, 6), (2, 4, 2), (3, 4, 2)] >>> find_shortest_even_path(4, 4, edges, 1, 4) '1 2 4'
answer:import heapq from collections import defaultdict def find_shortest_even_path(N, M, edges, A, B): graph = defaultdict(list) for u, v, w in edges: graph[u].append((v, w)) graph[v].append((u, w)) # Priority queue for Dijkstra's algorithm pq = [(0, 0, A, [])] # (total_weight, number_of_edges, current_node, path) visited = set() while pq: total_weight, num_edges, node, path = heapq.heappop(pq) if (node, total_weight % 2) in visited: continue visited.add((node, total_weight % 2)) path = path + [node] if node == B and total_weight % 2 == 0: return ' '.join(map(str, path)) for neighbor, weight in graph[node]: if (neighbor, (total_weight + weight) % 2) not in visited: heapq.heappush(pq, (total_weight + weight, num_edges + 1, neighbor, path)) return "-1" # Example input edges = [ (1, 2, 2), (1, 3, 4), (2, 3, 1), (2, 4, 7), (3, 5, 3), (4, 5, 1) ] print(find_shortest_even_path(5, 6, edges, 1, 5)) # Output: "1 2 3 5"