Find the Minimum and Maximum Number of Nodes Between Critical Points

Medium Topics Company Tags

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:

Find the Minimum and Maximum Number of Nodes Between Critical Points, example 1: illustration of the example

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:

Find the Minimum and Maximum Number of Nodes Between Critical Points, example 2: illustration of the example

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:

Find the Minimum and Maximum Number of Nodes Between Critical Points, example 3: illustration of the example

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 2 and 100000 nodes.
  • 1 <= Node.val <= 100000


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.