Skip to content
🤔prompts chat🧠
🔍
question:You are given a list of `n` movies, where each movie is represented by an integer `duration` which denotes its duration in minutes. Additionally, you have an integer `flightDuration` which represents the total duration of a flight in minutes. You want to watch exactly two different movies on the flight such that the total duration of the two movies is the maximum possible but does not exceed the `flightDuration` minus 30 minutes (because you need 30 minutes for take-off and landing). Return _the indices of the two movies you will watch_. If there are multiple answers, return any one of them. If no such pair of movies exists, return an empty list.

answer:def find_movies_pair(durations, flightDuration): Returns the indices of the two movies whose total duration is the maximum possible but does not exceed the flightDuration minus 30 minutes. durations: List[int] - List of movie durations in minutes. flightDuration: int - Total duration of the flight in minutes. return: List[int] - Indices of the two movies. max_duration = flightDuration - 30 best_pair = [] best_time = 0 durations_with_indices = list(enumerate(durations)) # Sort the list of movie durations with their corresponding indices durations_with_indices.sort(key=lambda x: x[1]) left, right = 0, len(durations) - 1 while left < right: time_left = durations_with_indices[left][1] time_right = durations_with_indices[right][1] current_time = time_left + time_right if current_time > max_duration: right -= 1 else: if current_time > best_time: best_time = current_time best_pair = [durations_with_indices[left][0], durations_with_indices[right][0]] left += 1 return best_pair

question:A linked list of integers is given. Write a function that removes all nodes that have duplicate integers, leaving only distinct numbers from the original list. Return the linked list sorted as well. - class ListNode { int val; ListNode next; ListNode() {} ListNode(int val) { this.val = val; } ListNode(int val, ListNode next) { this.val = val; this.next = next; } } - For example: Given the linked list `1 -> 2 -> 3 -> 3 -> 4 -> 4 -> 5`, return `1 -> 2 -> 5`.

answer:class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def remove_duplicates_unsorted_linked_list(head): Removes all nodes that have duplicate integers, leaving only distinct numbers from the original list. The list will be sorted in ascending order before returning. if not head: return None # count occurrences of each value count = {} current = head while current: count[current.val] = count.get(current.val, 0) + 1 current = current.next # collect distinct values distinct_values = [val for val, occ in count.items() if occ == 1] # if no distinct values found, return empty list if not distinct_values: return None # sort distinct values distinct_values.sort() # create new linked list new_head = ListNode(distinct_values[0]) current = new_head for val in distinct_values[1:]: current.next = ListNode(val) current = current.next return new_head

question:You are given a string `s` consisting of lowercase English letters. You need to construct a new string by rearranging the letters of `s` such that no two adjacent characters are the same. If it is not possible to do so, return an empty string. If there are multiple valid rearrangements, return any one of them. Write a function `reorganizeString(s: str) -> str` that achieves the objective.

answer:import heapq from collections import Counter def reorganizeString(s: str) -> str: Rearranges the string so that no two adjacent characters are the same. If it is not possible, returns an empty string. count = Counter(s) max_heap = [(-freq, char) for char, freq in count.items()] heapq.heapify(max_heap) prev_char, prev_freq = None, 0 result = [] while max_heap: freq, char = heapq.heappop(max_heap) result.append(char) if prev_freq < 0: heapq.heappush(max_heap, (prev_freq, prev_char)) prev_char, prev_freq = char, freq + 1 result_str = "".join(result) if len(result_str) != len(s): return "" return result_str

question:You are given a binary tree where each node contains an integer value. A **valid path** is defined as a path that starts at the root and ends at any leaf node, such that the sum of the values along the path is equal to a given integer `targetSum`. Write a function to determine if there exists at least one valid path in the binary tree. Return `true` if such a path exists, otherwise return `false`.

answer:class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def hasPathSum(root, targetSum): Determines if there exists a root-to-leaf path in the binary tree that sums to targetSum. Parameters: root (TreeNode): The root of the binary tree. targetSum (int): The sum to check for the valid path. Returns: bool: True if such a path exists, False otherwise. if not root: return False # If we reach a leaf node, check if the remaining targetSum equals the node's value if not root.left and not root.right: return root.val == targetSum # Recursively check the left and right subtrees targetSum -= root.val return hasPathSum(root.left, targetSum) or hasPathSum(root.right, targetSum)

Released under the chat License.

has loaded