In a linked list, we need to identify critical points - nodes that represent either local maxima or local minima when compared to their neighboring nodes.
A node is considered a local maximum if its value is strictly greater than both its previous and next nodes. Similarly, a node is a local minimum if its value is strictly smaller than both neighbors.
Important: A node can only be critical if it has both a previous and next node - meaning the first and last nodes of the linked list can never be critical points.
Your task is to find:
- The minimum distance between any two distinct critical points
- The maximum distance between any two distinct critical points
Return these as an array [minDistance, maxDistance]. If there are fewer than two critical points, return [-1, -1].
Example: For the linked list 5 → 3 → 1 → 2 → 5 → 1 → 2, the critical points are at positions 1 (local max: 3), 2 (local min: 1), and 5 (local min: 1). The minimum distance between critical points is 1, and the maximum distance is 4.
Input & Output
Constraints
- The number of nodes in the list is in the range [2, 105]
- 1 ≤ Node.val ≤ 105
- Important: Only nodes with both previous and next neighbors can be critical points