A node is a critical point if its value is strictly greater than both neighboring values, or strictly smaller than both. The first and last nodes cannot be critical points because each lacks one neighbor. Return [minimumDistance, maximumDistance] over all pairs of distinct critical points. Distance is the difference between their positions, so adjacent nodes have distance 1. If fewer than two critical points exist, return [-1,-1]. Input arrays list the node values from head to tail.
Filled nodes in the diagrams mark critical points.
Example 1:
Input: head = [1,4,2,5,1]
Output: [1,2]Explanation: Using zero-based positions, nodes 1, 2, and 3 are critical points. Adjacent critical points are one position apart, and the first and last are two positions apart.
Example 2:
Input: head = [2,2,3,3,2]
Output: [-1,-1]Explanation: No interior node is strictly greater or smaller than both neighbors: every potential peak has an equal neighbor. There are fewer than two critical points.
Example 3:
Input: head = [1,3,2,2,4,1]
Output: [3,3]Explanation: The critical points are at positions 1 and 4. Their distance is 3, so both the minimum and maximum distances are 3.
Constraints:
- The list contains between
2and100000nodes. 1 <= Node.val <= 100000