Tap here to switch tabs
Problem
Submissions

Problem: Min and Max Number of Nodes Between Critical Points

Try to solve the Min and Max Number of Nodes Between Critical Points problem.

Statement

Given the head of a singly linked list, determine the minimum and maximum distances between any two critical points.

A node is defined as a critical point if it is either a local maximum or a local minimum:

  • A node is a local maximum if its value is strictly greater than the value of the previous node and strictly greater than the value of the next node.

  • A node is a local minimum if its value is strictly smaller than the value of the previous node and strictly smaller than the value of the next node.

Return an integer array of length 22 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 22 critical points, return [-1, -1].

Note: A node can only be considered a critical point if it has both a preceding node and a succeeding node.

Constraints:

  • The number of nodes in the list is in the range [2,105][2, 10^5].

  • 1≤1 \leq Node.val ≤105\leq 10^5

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Min and Max Number of Nodes Between Critical Points

Try to solve the Min and Max Number of Nodes Between Critical Points problem.

Statement

Given the head of a singly linked list, determine the minimum and maximum distances between any two critical points.

A node is defined as a critical point if it is either a local maximum or a local minimum:

  • A node is a local maximum if its value is strictly greater than the value of the previous node and strictly greater than the value of the next node.

  • A node is a local minimum if its value is strictly smaller than the value of the previous node and strictly smaller than the value of the next node.

Return an integer array of length 22 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 22 critical points, return [-1, -1].

Note: A node can only be considered a critical point if it has both a preceding node and a succeeding node.

Constraints:

  • The number of nodes in the list is in the range [2,105][2, 10^5].

  • 1≤1 \leq Node.val ≤105\leq 10^5