> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Find the Minimum and Maximum Number of Nodes

> Tested Python solution for LeetCode 2058 with 22 pytest cases. Generate a practice environment with lcpy.

LeetCode 2058, [Medium](/catalog/medium). Topics: [Linked List](/catalog/topics/linked-list). [View on LeetCode](https://leetcode.com/problems/find-the-minimum-and-maximum-number-of-nodes-between-critical-points/description/).

Generate this problem as a practice environment: tested reference solution, 22 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2058   # by problem number
lcpy gen -s find_the_minimum_and_maximum_number_of_nodes_between_critical_points   # by problem name
```

## Problem

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

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/10/13/a1.png)

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

![Example 2](https://assets.leetcode.com/uploads/2021/10/13/a2.png)

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

![Example 3](https://assets.leetcode.com/uploads/2021/10/14/a5.png)

```
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`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/find_the_minimum_and_maximum_number_of_nodes_between_critical_points/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/find_the_minimum_and_maximum_number_of_nodes_between_critical_points/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from leetcode_py import ListNode


class Solution:
    # Time: O(n)
    # Space: O(1)
    def nodes_between_critical_points(self, head: ListNode[int] | None) -> list[int]:
        first = prev = 0
        min_gap = 10**6
        count = 0
        pos = 1
        if head is None or head.next is None or head.next.next is None:
            return [-1, -1]
        prev_val = head.val
        curr = head.next
        while curr.next is not None:
            next_val = curr.next.val
            if (curr.val > prev_val and curr.val > next_val) or (
                curr.val < prev_val and curr.val < next_val
            ):
                if count == 0:
                    first = pos
                else:
                    min_gap = min(min_gap, pos - prev)
                prev = pos
                count += 1
            prev_val = curr.val
            curr = curr.next
            pos += 1
        if count < 2:
            return [-1, -1]
        return [min_gap, prev - first]
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.