Skip to content
🤔prompts chat🧠
🔍
question:def longest_sequence_of_minute_intervals(T: int, test_cases: List[Tuple[int, List[int]]]) -> List[int]: Given a number of test cases T and a list of test cases, each containing a number of events and their corresponding timestamps, returns a list of integers representing the length of the longest sequence of events where each event occurs exactly one minute after the previous one. >>> longest_sequence_of_minute_intervals(2, [(5, [1, 2, 3, 5, 6]), (4, [10, 12, 13, 14])]) [3, 3] >>> longest_sequence_of_minute_intervals(1, [(3, [0, 1, 2])]) [3] >>> longest_sequence_of_minute_intervals(1, [(5, [5, 7, 8, 9, 10])]) [4] >>> longest_sequence_of_minute_intervals(3, [(4, [2, 3, 4, 5]), (3, [7, 9, 10]), (2, [15, 16])]) [4, 2, 2]

answer:def longest_sequence_of_minute_intervals(T, test_cases): results = [] for case in test_cases: n, timestamps = case if n <= 1: results.append(n) continue longest_length = 1 current_length = 1 for i in range(1, n): if timestamps[i] == timestamps[i-1] + 1: current_length += 1 longest_length = max(longest_length, current_length) else: current_length = 1 results.append(longest_length) return results

question:def single_number(nums): Returns the single number in the list which appears only once, while other numbers appear exactly twice. >>> single_number([1, 2, 3, 2, 1]) == 3 >>> single_number([4, 1, 2, 1, 2, 4, 5]) == 5 >>> single_number([2, 2, 3, 5, 3]) == 5 >>> single_number([7, 3, 5, 3, 5]) == 7 >>> single_number([10]) == 10 >>> single_number([0, 0, 1, 1, -1, -1, 99]) == 99 >>> single_number([17, 23, 17, 19, 19]) == 23 >>> single_number([1000000, 1, 1, 2, 2, 1000000, 99]) == 99 >>> single_number([-2, -2, -3]) == -3 >>> large_input = list(range(1, 500001)) + list(range(1, 500000)) >>> single_number(large_input) == 500000

answer:def single_number(nums): Returns the single number in the list which appears only once, while other numbers appear exactly twice. single = 0 for num in nums: single ^= num return single

question:def find_smallest_missing_positive(t: int, test_cases: List[Tuple[int, List[int]]]) -> List[int]: Given an integer t representing the number of test cases and a list of tuples where each tuple contains an integer N and a list of N integers, find the smallest positive integer that is not present in the array for each test case. >>> find_smallest_missing_positive(2, [(3, [1, 2, 0]), (4, [3, 4, -1, 1])]) [3, 2] >>> find_smallest_missing_positive(1, [(5, [2, 3, 7, 6, 8, -1, -10, 15])]) [1] >>> find_smallest_missing_positive(1, [(4, [-1, -2, -3, -4])]) [1] >>> find_smallest_missing_positive(1, [(6, [1, 2, 3, 4, 5, 6])]) [7] >>> find_smallest_missing_positive(2, [(3, [1, 1, 0]), (3, [1, 2, 3])]) [2, 4]

answer:def find_smallest_missing_positive(t, test_cases): results = [] for i in range(t): n, array = test_cases[i][0], test_cases[i][1] present = [False] * (n + 1) for num in array: if 1 <= num <= n: present[num] = True smallest_missing_positive = n + 1 for j in range(1, n + 1): if not present[j]: smallest_missing_positive = j break results.append(smallest_missing_positive) return results

question:def countDistinctIslands(grid: List[List[int]]) -> int: Returns the number of distinct islands in the given grid. >>> countDistinctIslands([[1, 1, 0, 0, 0], [1, 1, 0, 0, 1], [0, 0, 0, 1, 1], [0, 0, 0, 1, 1]]) 2 >>> countDistinctIslands([[1, 1, 0, 0, 0], [1, 0, 0, 0, 1], [0, 0, 0, 1, 1], [0, 0, 0, 0, 0], [0, 1, 1, 0, 0]]) 3 >>> countDistinctIslands([[1, 0], [0, 0]]) 1 >>> countDistinctIslands([[0, 0, 0], [0, 0, 0], [0, 0, 0]]) 0 >>> countDistinctIslands([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) 1 pass

answer:def countDistinctIslands(grid): Returns the number of distinct islands in the given grid. grid: List of List of integers (1's and 0's) if not grid or not grid[0]: return 0 def dfs(x, y): if x < 0 or y < 0 or x >= len(grid) or y >= len(grid[0]) or grid[x][y] == 0: return grid[x][y] = 0 dfs(x + 1, y) dfs(x - 1, y) dfs(x, y + 1) dfs(x, y - 1) def find_islands(): count = 0 for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] == 1: dfs(i, j) count += 1 return count return find_islands()

Released under the chat License.

has loaded