Find the Minimum and Maximum Number of Nodes Between Critical Points

A critical point in a linked list is a local maxima or a local minima. A node is a local maxima if its value is strictly greater than both the previous and the next node, and a local minima if it is strictly smaller than both. A node can only be critical if it has both a previous and a next node. Given the head of a linked list, return [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].

30:00

Find the Minimum and Maximum Number of Nodes Between Critical Points
medium
Free written solution
Topics
Companies

A critical point in a linked list is a local maxima or a local minima. A node is a local maxima if its value is strictly greater than both the previous and the next node, and a local minima if it is strictly smaller than both. A node can only be critical if it has both a previous and a next node. Given the head of a linked list, return [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]
Constraints:
  • The number of nodes in the list is in the range [2,105][2, 10^5].

  • 1≤Node.val≤1051 \leq \text{Node.val} \leq 10^5

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"head":[3,1]}

A node is critical if it is strictly higher (a local maximum) or strictly lower (a local minimum) than both neighbors. The head and tail have only one neighbor, so they never count.

first-1
last-1
minDistinf
maxDist?
3011critical indices
▭ prev, curr, next▲ local max▼ local min— closest pair— first to last
Variables
VariableValue
i-
first-1
last-1
minDistinf
DepthFunction Call
Stack empty
0/3