Appearance
question:def largest_same_animal_area(n: int, m: int, grid: List[str]) -> int: Find the largest rectangular area on the grid such that all animals in that rectangular area are of the same type. Args: n : int : number of rows in the grid m : int : number of columns in the grid grid : List[str] : grid representing the farm with animals and empty cells Returns: int : the area of the largest rectangle that contains only one type of animal Examples: >>> largest_same_animal_area(4, 5, ["aabba", "abbba", "aabba", "bbbaa"]) 6 >>> largest_same_animal_area(3, 3, ["ccc", "c.c", "ccc"]) 3
answer:def largest_same_animal_area(n, m, grid): def calculate_max_histogram_area(histogram): stack = [] max_area = 0 index = 0 while index < len(histogram): if not stack or histogram[stack[-1]] <= histogram[index]: stack.append(index) index += 1 else: top_of_stack = stack.pop() area = (histogram[top_of_stack] * ((index - stack[-1] - 1) if stack else index)) max_area = max(max_area, area) while stack: top_of_stack = stack.pop() area = (histogram[top_of_stack] * ((index - stack[-1] - 1) if stack else index)) max_area = max(max_area, area) return max_area max_area = 0 for animal in 'abcdefghijklmnopqrstuvwxyz': height = [0] * m for i in range(n): for j in range(m): if grid[i][j] == animal: height[j] += 1 else: height[j] = 0 max_area = max(max_area, calculate_max_histogram_area(height)) return max_area
question:def isSquareFree(n: int) -> bool: Returns True if n is a square-free number, False otherwise. >>> isSquareFree(15) True >>> isSquareFree(18) False >>> isSquareFree(31) True >>> isSquareFree(45) False >>> isSquareFree(1) True >>> isSquareFree(1000000000) False >>> isSquareFree(2) True >>> isSquareFree(4) False >>> isSquareFree(1000003) True
answer:def isSquareFree(n): Returns True if n is a square-free number, False otherwise. # The largest integer k such that k^2 <= n k = int(n**0.5) # Check divisibility by squares of all integers from 2 to k for i in range(2, k + 1): if n % (i * i) == 0: return False return True
question:def minimum_bombs_to_destroy_ring(t: int, test_cases: List[int]) -> List[int]: Determine the minimum number of bombs required to destroy all cells in the ring. Args: t : int : The number of test cases. test_cases : List[int] : A list of integers representing the size of each circular ring. Returns: List[int] : A list of integers representing the minimum number of bombs required for each test case. >>> minimum_bombs_to_destroy_ring(3, [4, 7, 6]) [2, 3, 2] >>> minimum_bombs_to_destroy_ring(1, [3]) [1] >>> minimum_bombs_to_destroy_ring(1, [10]) [4]
answer:def minimum_bombs_to_destroy_ring(t, test_cases): results = [] for n in test_cases: # Calculate the minimum number of bombs if n % 3 == 0: min_bombs = n // 3 else: min_bombs = (n // 3) + 1 results.append(min_bombs) return results
question:def longest_palindrome(s: str) -> str: Find the longest palindromic substring in the given string s. If the length of s is 0, return an empty string. >>> longest_palindrome("babad") in ["bab", "aba"] True >>> longest_palindrome("cbbd") == "bb" True >>> longest_palindrome("a") == "a" True >>> longest_palindrome("ac") in ["a", "c"] True
answer:def longest_palindrome(s): This function returns the longest palindromic substring in the given string s. If the length of s is 0, the function returns an empty string. if len(s) == 0: return "" def expand_from_center(left, right): Helper function to expand from the center and find the longest palindrome while left >= 0 and right < len(s) and s[left] == s[right]: left -= 1 right += 1 return s[left + 1:right] longest_palindromic_substring = "" for i in range(len(s)): # Odd length palindromes substring1 = expand_from_center(i, i) if len(substring1) > len(longest_palindromic_substring): longest_palindromic_substring = substring1 # Even length palindromes substring2 = expand_from_center(i, i + 1) if len(substring2) > len(longest_palindromic_substring): longest_palindromic_substring = substring2 return longest_palindromic_substring