Mid/SeniorLinked List

Find the Minimum and Maximum Number of Nodes Between Critical Points

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
[3] -> [1] -> null
Inputhead = [3,1]
Output[-1,-1]
There are no critical points in [3, 1].
Example 2
[5] -> [3] -> [1] -> [2] -> [5] -> [1] -> [2] -> null
Inputhead = [5,3,1,2,5,1,2]
Output[1,3]
The third node is a local minima, the fifth node is a local maxima, and the sixth node is a local minima, so the minimum distance is 6 - 5 = 1 and the maximum distance is 6 - 3 = 3.

Constraints

  • The number of nodes in the list is in the range [2, 10^5].
  • 1 <= Node.val <= 10^5

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate