Appearance
question:from typing import List def can_reach_last_tank(tanks: List[int]) -> bool: Determine if you can reach the last tank given a list of fuel in different tanks. You start at the first tank and want to reach the last tank. Each tank can be used to jump to another tank that is at most a certain number of positions away, based on the amount of fuel in the current tank. Examples: >>> can_reach_last_tank([2, 3, 1, 1, 4]) True >>> can_reach_last_tank([3, 2, 1, 0, 4]) False def test_can_reach_last_tank(): assert can_reach_last_tank([2, 3, 1, 1, 4]) == True assert can_reach_last_tank([3, 2, 1, 0, 4]) == False assert can_reach_last_tank([0]) == True assert can_reach_last_tank([2, 0, 0, 0, 0]) == False assert can_reach_last_tank([1, 1, 1, 1, 1]) == True assert can_reach_last_tank([5, 0, 0, 0, 0]) == True
answer:from typing import List def can_reach_last_tank(tanks: List[int]) -> bool: max_reach = 0 n = len(tanks) for i in range(n): if i > max_reach: return False max_reach = max(max_reach, i + tanks[i]) if max_reach >= n - 1: return True return False
question:from typing import List def diagonalOrder(matrix: List[List[int]]) -> List[int]: Returns the diagonals of a matrix in a zigzag order. >>> diagonalOrder([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) [1, 2, 4, 7, 5, 3, 6, 8, 9] >>> diagonalOrder([[1, 2], [3, 4], [5, 6]]) [1, 2, 3, 5, 4, 6] >>> diagonalOrder([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) [1, 2, 5, 9, 6, 3, 4, 7, 10, 11, 8, 12] >>> diagonalOrder([[5, 6], [7, 8]]) [5, 6, 7, 8] >>> diagonalOrder([[1]]) [1]
answer:from typing import List def diagonalOrder(matrix: List[List[int]]) -> List[int]: if not matrix or not matrix[0]: return [] rows, cols = len(matrix), len(matrix[0]) result = [] for d in range(rows + cols - 1): if d % 2 == 0: # Collect upward diagonal (bottom to top) r = min(d, rows - 1) c = d - r while r >= 0 and c < cols: result.append(matrix[r][c]) r -= 1 c += 1 else: # Collect downward diagonal (top to bottom) c = min(d, cols - 1) r = d - c while c >= 0 and r < rows: result.append(matrix[r][c]) r += 1 c -= 1 return result
question:def can_form_word(grid: List[List[str]], word: str) -> bool: Determine if the word can be formed by consecutively adjacent cells in the grid. >>> can_form_word([ ... ['A', 'B', 'C', 'E'], ... ['S', 'F', 'C', 'S'], ... ['A', 'D', 'E', 'E']], "ABCCED") True >>> can_form_word([ ... ['A', 'B', 'C', 'E'], ... ['S', 'F', 'C', 'S'], ... ['A', 'D', 'E', 'E']], "SEE") True >>> can_form_word([ ... ['A', 'B', 'C', 'E'], ... ['S', 'F', 'C', 'S'], ... ['A', 'D', 'E', 'E']], "ABCB") False
answer:def can_form_word(grid, word): if not grid or not word: return False rows, cols = len(grid), len(grid[0]) def dfs(r, c, index): if index == len(word): return True if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] != word[index]: return False temp = grid[r][c] grid[r][c] = '#' found = (dfs(r+1, c, index+1) or dfs(r, c+1, index+1) or dfs(r-1, c, index+1) or dfs(r, c-1, index+1)) grid[r][c] = temp return found for row in range(rows): for col in range(cols): if dfs(row, col, 0): return True return False # Example use case grid = [ ['A', 'B', 'C', 'E'], ['S', 'F', 'C', 'S'], ['A', 'D', 'E', 'E'] ] word1 = "ABCCED" word2 = "SEE" word3 = "ABCB" print(can_form_word(grid, word1)) # Output: True print(can_form_word(grid, word2)) # Output: True print(can_form_word(grid, word3)) # Output: False
question:def min_unattractiveness(T: int, test_cases: List[Tuple[int, int, List[int]]]) -> List[int]: Returns the minimum unattractiveness for each test case. :param T: Integer, the number of test cases. :param test_cases: List of tuples, where each tuple contains: - N, M: Integers, dimensions of the grid. - colors: List of integers, the colors on tiles in the bag. :return: List of integers, the minimum unattractiveness for each test case. >>> T = 2 >>> test_cases = [(2, 2, [1, 2, 3, 4]), (3, 3, [7, 9, 8, 6, 1, 3, 4, 2, 5])] >>> min_unattractiveness(T, test_cases) [1, 1]
answer:def min_unattractiveness(T, test_cases): Returns the minimum unattractiveness for each test case. :param T: Integer, the number of test cases. :param test_cases: List of tuples, where each tuple contains: - N, M: Integers, dimensions of the grid. - colors: List of integers, the colors on tiles in the bag. :return: List of integers, the minimum unattractiveness for each test case. results = [] for test in test_cases: N, M, colors = test colors.sort() # In sorted order, the minimum difference between any two adjacent tiles is minimized. min_diff = float('inf') for i in range(1, len(colors)): min_diff = min(min_diff, abs(colors[i] - colors[i-1])) results.append(min_diff) return results