Skip to content

Practice Find the Minimum and Maximum Number of Nodes Between Critical Points

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2058
LeetCode Problem

Find the Minimum and Maximum Number of Nodes Between Critical Points

Problem Statement

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].

Source: Find the Minimum and Maximum Number of Nodes Between Critical Points on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
head = [3,1]
Output
[-1,-1]

# Example 2

Input
head = [5,3,1,2,5,1,2]
Output
[1,3]

# Example 3

Input
head = [1,3,2,2,3,2,2,2,7]
Output
[3,3]

Constraints

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

Related LeetCode Problems

Practice more problems similar to Find the Minimum and Maximum Number of Nodes Between Critical Points.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.