Skip to content
🤔prompts chat🧠
🔍
question:def count_islands(grid: List[List[int]]) -> int: Counts the number of islands in the given grid. An island is a maximal group of 1s connected vertically or horizontally. Args: grid (List[List[int]]): The M x N grid containing 1s for land and 0s for water. Returns: int: The number of islands in the grid. >>> count_islands([ ... [1, 1, 0, 0, 0], ... [1, 1, 0, 1, 1], ... [0, 0, 0, 0, 0], ... [0, 0, 1, 1, 0] ... ]) 3 >>> count_islands([ ... [0, 0, 0, 0, 0] ... ]) 0 >>> count_islands([ ... [1] ... ]) 1

answer:def count_islands(grid): Counts the number of islands in the given grid. if not grid: return 0 M, N = len(grid), len(grid[0]) visited = [[False for _ in range(N)] for _ in range(M)] def dfs(r, c): if r < 0 or r >= M or c < 0 or c >= N or grid[r][c] == 0 or visited[r][c]: return visited[r][c] = True # Explore all four directions dfs(r + 1, c) dfs(r - 1, c) dfs(r, c + 1) dfs(r, c - 1) island_count = 0 for i in range(M): for j in range(N): if grid[i][j] == 1 and not visited[i][j]: dfs(i, j) island_count += 1 return island_count

question:def count_unique_items_per_customer(M: int, N: int, customer_carts: List[List[int]]) -> List[int]: Returns a list containing the number of unique items in each customer's cart. Parameters: M (int): The number of customers. N (int): The maximum number of unique items in the cart. customer_carts (list of list of int): A list where each sublist represents items added to a customer's cart. Returns: list of int: A list where each element is the number of unique items in the corresponding customer's cart. >>> count_unique_items_per_customer(3, 5, [[1, 2, 3, 4, 5], [2, 3, 4, 4, 4], [5, 5, 5, 5, 5]]) [5, 3, 1] >>> count_unique_items_per_customer(2, 4, [[1, 2, 3, 4], [4, 4, 4, 4]]) [4, 1]

answer:def count_unique_items_per_customer(M, N, customer_carts): Returns a list containing the number of unique items in each customer's cart. Parameters: M (int): The number of customers. N (int): The maximum number of unique items in the cart (not necessarily needed in the function). customer_carts (list of list of int): A list where each sublist represents items added to a customer's cart. Returns: list of int: A list where each element is the number of unique items in the corresponding customer's cart. unique_counts = [] for cart in customer_carts: unique_counts.append(len(set(cart))) return unique_counts # Example input M = 3 N = 5 customer_carts = [ [1, 2, 3, 4, 5], [2, 3, 4, 4, 4], [5, 5, 5, 5, 5] ] print(count_unique_items_per_customer(M, N, customer_carts))

question:from typing import List, Tuple def mergeTasks(tasksPaul: List[Tuple[str, int]], tasksClara: List[Tuple[str, int]]) -> List[str]: Merge two lists of tasks based on their priority while maintaining the order of tasks with the same priority from the same person. Args: tasksPaul (List[Tuple[str, int]]): List of tuples representing Paul's tasks. tasksClara (List[Tuple[str, int]]): List of tuples representing Clara's tasks. Returns: List[str]: List of task descriptions in the merged and sorted order. Example: >>> mergeTasks([("Task 1", 3), ("Task 2", 1), ("Task 3", 2)], [("Task A", 2), ("Task B", 1), ("Task C", 3)]) ["Task 2", "Task B", "Task 3", "Task A", "Task 1", "Task C"]

answer:from typing import List, Tuple def mergeTasks(tasksPaul: List[Tuple[str, int]], tasksClara: List[Tuple[str, int]]) -> List[str]: all_tasks = tasksPaul + tasksClara # Combine the lists all_tasks.sort(key=lambda x: x[1]) # Sort by priority return [task[0] for task in all_tasks] # Extract task descriptions # Example usage tasksPaul = [("Task 1", 3), ("Task 2", 1), ("Task 3", 2)] tasksClara = [("Task A", 2), ("Task B", 1), ("Task C", 3)] merged_tasks = mergeTasks(tasksPaul, tasksClara) print(merged_tasks) # Output should be: ["Task 2", "Task B", "Task 3", "Task A", "Task 1", "Task C"]

question:def can_rearrange_string(N: int, s: str) -> str: Determine if the string can be rearranged such that no two adjacent characters are the same. Parameters: N (int): length of the string s (str): string consisting of only lower case alphabetical letters Returns: str: "YES" if the string can be rearranged, "NO" otherwise Example: >>> can_rearrange_string(5, "aabbc") "YES" >>> can_rearrange_string(5, "aaaab") "NO" from collections import Counter def test_can_rearrange_string(): assert can_rearrange_string(5, "aabbc") == "YES" assert can_rearrange_string(5, "aaaab") == "NO" assert can_rearrange_string(7, "aabbccd") == "YES" assert can_rearrange_string(2, "aa") == "NO" assert can_rearrange_string(1, "a") == "YES" assert can_rearrange_string(6, "aaabbc") == "YES" assert can_rearrange_string(6, "aaaabc") == "NO" if __name__ == "__main__": test_can_rearrange_string()

answer:def can_rearrange_string(N, s): Determine if the string can be rearranged such that no two adjacent characters are the same. Parameters: N (int): length of the string s (str): string consisting of only lower case alphabetical letters Returns: str: "YES" if the string can be rearranged, "NO" otherwise from collections import Counter freq = Counter(s) max_freq = max(freq.values()) # no of remaining characters remaining = N - max_freq if max_freq - 1 > remaining: return "NO" else: return "YES"

Released under the chat License.

has loaded