Find the Minimum and Maximum Number of Nodes Between Critical Points - Problem

In a linked list, we need to identify critical points - nodes that represent either local maxima or local minima when compared to their neighboring nodes.

A node is considered a local maximum if its value is strictly greater than both its previous and next nodes. Similarly, a node is a local minimum if its value is strictly smaller than both neighbors.

Important: A node can only be critical if it has both a previous and next node - meaning the first and last nodes of the linked list can never be critical points.

Your task is to find:

  • The minimum distance between any two distinct critical points
  • The maximum distance between any two distinct critical points

Return these as an array [minDistance, maxDistance]. If there are fewer than two critical points, return [-1, -1].

Example: For the linked list 5 → 3 → 1 → 2 → 5 → 1 → 2, the critical points are at positions 1 (local max: 3), 2 (local min: 1), and 5 (local min: 1). The minimum distance between critical points is 1, and the maximum distance is 4.

Input & Output

example_1.py — Basic case with multiple critical points
$ Input: [5,3,1,2,5,1,2]
Output: [1,4]
💡 Note: Critical points are at indices 1 (value 3, local max), 2 (value 1, local min), and 5 (value 1, local min). The minimum distance between any two critical points is 1 (between indices 1 and 2), and the maximum distance is 4 (between indices 1 and 5).
example_2.py — Only two critical points
$ Input: [1,3,2,2,3,2,2,2,7]
Output: [3,3]
💡 Note: Critical points are at indices 1 (value 3, local max) and 4 (value 3, local max). Since there are only two critical points, both minimum and maximum distances are the same: 3.
example_3.py — No critical points possible
$ Input: [2,3]
Output: [-1,-1]
💡 Note: The linked list has only 2 nodes, so there are no nodes that can have both previous and next neighbors. Therefore, no critical points can exist.

Constraints

  • The number of nodes in the list is in the range [2, 105]
  • 1 ≤ Node.val ≤ 105
  • Important: Only nodes with both previous and next neighbors can be critical points

Visualization

Tap to expand
🏔️ Mountain Range Critical Points AnalysisPEAKFirst CriticalVALLEYDistance: 1VALLEYLast CriticalMin: 1Max: 4🎯 Key Insight: Track first and previous critical points to calculate distances efficientlyMaximum distance = Last - First Critical Point | Minimum distance = Smallest gap between consecutive points
Understanding the Visualization
1
Identify Terrain Features
Walk through the mountain range with three position markers (previous, current, next) to identify peaks and valleys
2
Track Key Landmarks
Remember the first critical point found and keep updating the most recent one
3
Measure Distances
Calculate the shortest path between adjacent critical points and the longest path from first to last
Key Takeaway
🎯 Key Insight: By maintaining just two variables (first and previous critical point positions), we can solve this problem in O(n) time with O(1) space complexity, avoiding the need to store all critical point positions.
Asked in
Amazon 45 Microsoft 32 Google 28 Meta 21
52.3K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen