A critical point in a linked list is defined as either a local maxima or a local minima.
A node is a local maxima if the current node has a value strictly greater than the previous node and the next node.
A node is a local minima if the current node has a value strictly smaller than the previous node and the next node.
Note that a node can only be a local maxima/minima if there exists both a previous node and a next node.
Given a linked list head, return an array of length 2 containing [minDistance, maxDistance] where minDistance is the minimum distance between any two distinct critical points and maxDistance is the maximum distance between any two distinct critical points. If there are fewer than two critical points, return [-1, -1].
Example 1:
Input: head = [3,1] Output: [-1,-1] Explanation: There are no critical points in [3,1].
Example 2:
Input: head = [5,3,1,2,5,1,2] Output: [1,3] Explanation: There are three critical points: - [5,3,1,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2. - [5,3,1,2,5,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1. - [5,3,1,2,5,1,2]: The sixth node is a local minima because 1 is less than 5 and 2. The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1. The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3.
Example 3:
Input: head = [1,3,2,2,3,2,2,2,7] Output: [3,3] Explanation: There are two critical points: - [1,3,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2. - [1,3,2,2,3,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2. Both the minimum and maximum distances are between the second and the fifth node. Thus, minDistance and maxDistance is 5 - 2 = 3. Note that the last node is not considered a local maxima because it does not have a next node.
Constraints:
[2, 105].1 <= Node.val <= 105When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We're looking for special spots in a path, called 'critical points'. The brute force method involves checking every single position along the path to see if it's a critical point, and then measuring the distances between them to find the smallest and largest.
Here's how the algorithm would work step-by-step:
def find_min_max_distance_between_critical_points_brute_force(heights):
critical_points_indices = []
number_of_heights = len(heights)
# Find critical points.
for index in range(1, number_of_heights - 1):
if heights[index] > heights[index - 1] and heights[index] > heights[index + 1]:
critical_points_indices.append(index)
elif heights[index] < heights[index - 1] and heights[index] < heights[index + 1]:
critical_points_indices.append(index)
if len(critical_points_indices) < 2:
return [-1, -1]
minimum_distance = float('inf')
maximum_distance = 0
# Iterate through all pairs of critical points.
for first_critical_point_index in range(len(critical_points_indices)):
# Only consider critical points after the current point.
for second_critical_point_index in range(first_critical_point_index + 1, len(critical_points_indices)):
distance = abs(critical_points_indices[first_critical_point_index] - critical_points_indices[second_critical_point_index])
# Update the minimum distance if necessary
minimum_distance = min(minimum_distance, distance)
# Update the maximum distance if necessary
maximum_distance = max(maximum_distance, distance)
return [minimum_distance, maximum_distance]To find the smallest and largest distances between critical points in a sequence, we first need to identify these critical points. Then, instead of comparing every pair of critical points, we'll cleverly track the minimum and maximum distances as we go through the list once.
Here's how the algorithm would work step-by-step:
def find_min_max_critical(nodes):
critical_points = []
# Identify critical points, skipping the ends.
for i in range(1, len(nodes) - 1):
if (nodes[i] > nodes[i - 1] and nodes[i] > nodes[i + 1]) or \
(nodes[i] < nodes[i - 1] and nodes[i] < nodes[i + 1]):
critical_points.append(i)
# Not enough critical points, cannot determine min/max distances.
if len(critical_points) < 2:
return [-1, -1]
min_distance = float('inf')
max_distance = critical_points[-1] - critical_points[0]
# Iterate over critical points.
for i in range(len(critical_points) - 1):
#Calc distance to next point.
distance = critical_points[i + 1] - critical_points[i]
#Update min distance
min_distance = min(min_distance, distance)
return [min_distance, max_distance]| Case | How to Handle |
|---|---|
| Null or empty input list | Return [-1, -1] indicating no critical points found and thus no distance. |
| List with fewer than 3 elements | Return [-1, -1] because a critical point requires at least three elements to compare. |
| List with only one critical point | Return [-1, -1] because you need at least two critical points to calculate min/max distances. |
| List with all elements having the same value | Return [-1, -1] as there are no critical points possible. |
| Adjacent critical points | Ensure the minimum distance calculation correctly handles consecutive critical points with a distance of 1. |
| Critical points appearing at the beginning and end of the list | The algorithm should correctly identify and use these boundary critical points when calculating minimum and maximum distances. |
| Large input list leading to potential integer overflow when calculating distances | Use long integer type for storing distances to avoid overflow issues. |
| Input list contains duplicate values that create multiple critical points | The algorithm must accurately identify all valid critical points even if they are next to each other because of duplicate values. |