Appearance
question:You are given a string `s` consisting of lowercase letters. The string can be split into substrings using one or more palindromic substrings. A palindrome is a string that reads the same backward as forward. Find and return the minimum number of cuts needed to split the string into palindromic substrings.
answer:def min_cut_palindrome(s): Returns the minimum number of cuts needed to split the string into palindromic substrings. n = len(s) if n <= 1: return 0 # Create a DP table to check palindrome for substrings is_palindrome = [[False] * n for _ in range(n)] for i in range(n): is_palindrome[i][i] = True for length in range(2, n + 1): for i in range(n - length + 1): j = i + length - 1 if length == 2: is_palindrome[i][j] = (s[i] == s[j]) else: is_palindrome[i][j] = (s[i] == s[j] and is_palindrome[i + 1][j - 1]) # Create a DP array to find the minimum cuts cuts = [0] * n for i in range(n): if is_palindrome[0][i]: cuts[i] = 0 else: cuts[i] = float('inf') for j in range(i): if is_palindrome[j + 1][i] and cuts[j] + 1 < cuts[i]: cuts[i] = cuts[j] + 1 return cuts[-1]
question:You are given two integer arrays `arr1` and `arr2` of equal length `n`. Construct a new array `result` of length `n` where `result[i]` is the **minimum** of the sum of any non-empty subarray from `arr1` that starts at `i` and the sum of the subarray from `arr2` that starts at `i`. Return the array `result`.
answer:def min_subarray_sum(arr1, arr2): Returns an array where result[i] is the minimum of the sum of any non-empty subarray from arr1 that starts at i and the sum of the subarray from arr2 that starts at i. n = len(arr1) result = [0] * n for i in range(n): min_sum1 = float('inf') current_sum1 = 0 for j in range(i, n): current_sum1 += arr1[j] min_sum1 = min(min_sum1, current_sum1) min_sum2 = float('inf') current_sum2 = 0 for j in range(i, n): current_sum2 += arr2[j] min_sum2 = min(min_sum2, current_sum2) result[i] = min(min_sum1, min_sum2) return result
question:You are given an array `heights` representing the heights of students in a class. The array is **0-indexed** and its length is `n`. You are also given a positive integer `k`. You need to find the maximum number of students that can be grouped such that in each group, the difference between the tallest and the shortest student is no greater than `k`. Return _the maximum number of students that can be grouped according to the given condition_.
answer:def max_students_grouped(heights, k): Returns the maximum number of students that can be grouped such that in each group, the difference between the tallest and the shortest student is no greater than k. heights (list[int]): List representing heights of students. k (int): Maximum allowed difference in heights within a group. Returns: int: Maximum number of students that can be grouped according to the given condition. if not heights or k < 0: return 0 heights.sort() max_group = 0 left = 0 for right in range(len(heights)): while heights[right] - heights[left] > k: left += 1 max_group = max(max_group, right - left + 1) return max_group
question:You are given two integers `sandwiches` and `students`, representing the number of sandwiches and the number of students in a queue, respectively. Each student can either **only eat** circular sandwiches or square sandwiches. Each time, the sandwich at the front of the queue is given to the student at the front of the queue if they want it. If they don't, they go to the end of the queue. This process continues until there are no sandwiches left or no student in the queue can eat the remaining sandwiches. Each sandwich has a type `0` for circular or `1` for square, while students are represented similarly, where each student is `0` if they only eat circular sandwiches and `1` if they only eat square sandwiches. **Return the number of students that remain in the queue.**
answer:def count_students(sandwiches, students): This function returns the number of students that remain in the queue after the distribution of sandwiches is complete. Args: sandwiches (list): A list of integers representing the sandwiches. students (list): A list of integers representing the students. Returns: int: The number of students that remain in the queue. from collections import deque sandwich_queue = deque(sandwiches) student_queue = deque(students) while sandwich_queue and student_queue: attempts = 0 # To track if any student was able to get a sandwich in the last cycle while student_queue[0] != sandwich_queue[0]: student_queue.append(student_queue.popleft()) attempts += 1 if attempts == len(student_queue): return len(student_queue) sandwich_queue.popleft() student_queue.popleft() return len(student_queue)