Watch 10 video solutions for Find the Minimum and Maximum Number of Nodes Between Critical Points, a medium level problem involving Linked List. This walkthrough by codestorywithMIK has 7,164 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 <= 105Problem Overview: You are given a linked list and must identify all critical points. A node is critical if it is strictly greater than both neighbors (local maxima) or strictly smaller than both neighbors (local minima). After locating all such nodes, compute the minimum and maximum distance between any two critical points based on their positions in the list.
Approach 1: Single Pass and Collect Critical Points (O(n) time, O(1) space)
Traverse the linked list once while keeping track of the previous, current, and next node values. When the current value forms a local maximum or minimum, record its index as a critical point. Maintain the first critical index, the previous critical index, and update the minimum distance using the gap between consecutive critical points. The maximum distance is simply the difference between the first and the latest critical point. This works because the list is processed sequentially, so consecutive critical points are discovered in order.
This approach uses constant extra space since you only store a few index variables instead of all positions. It is the most efficient implementation and fits well with typical linked list traversal patterns.
Approach 2: Two-pass Approach for Clarity (O(n) time, O(k) space)
First iterate through the list and store the indices of all critical points in an array. After the traversal, compute the minimum distance by scanning adjacent elements in the array. The maximum distance is simply the difference between the first and last stored indices. Although this approach uses extra memory proportional to the number of critical points, it separates detection from distance calculation, which can make the logic easier to reason about.
This version is helpful when debugging or when you prefer clearer separation between scanning and computation steps. The algorithm still runs in linear time since each node is visited once.
Recommended for interviews: The single-pass solution is what most interviewers expect. It demonstrates strong understanding of linked list traversal and efficient state tracking. Implementing the two-pass version first can help verify the logic, but optimizing to a single traversal shows stronger algorithmic thinking.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Single Pass and Track Critical Indices | O(n) | O(1) | Best for interviews and production when you want optimal memory usage |
| Two-pass with Stored Critical Points | O(n) | O(k) | Useful for clarity or debugging when storing all critical indices simplifies logic |