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

# Odd Even Jump Python Solution with Tests

> Tested Python solution for LeetCode 975 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 975, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Dynamic Programming](/catalog/topics/dynamic-programming), [Stack](/catalog/topics/stack), [Sorting](/catalog/topics/sorting), [Monotonic Stack](/catalog/topics/monotonic-stack), [Ordered Set](/catalog/topics/ordered-set). [View on LeetCode](https://leetcode.com/problems/odd-even-jumps/description/).

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

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

## Problem

You are given an integer array `arr`. From some starting index, you can make a series of jumps. The (1st, 3rd, 5th, ...) jumps in the series are called odd-numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even-numbered jumps. Note that the jumps are numbered, not the indices.

You may jump forward from index `i` to index `j` (with `i < j`) in the following way:

* During odd-numbered jumps (i.e., jumps 1, 3, 5, ...), you jump to the index `j` such that `arr[i] <= arr[j]` and `arr[j]` is the smallest possible value. If there are multiple such indices `j`, you can only jump to the smallest such index `j`.
* During even-numbered jumps (i.e., jumps 2, 4, 6, ...), you jump to the index `j` such that `arr[i] >= arr[j]` and `arr[j]` is the largest possible value. If there are multiple such indices `j`, you can only jump to the smallest such index `j`.
* It may be the case that for some index `i`, there are no legal jumps.

A starting index is good if, starting from that index, you can reach the end of the array (index `arr.length - 1`) by jumping some number of times (possibly 0 or more than once).

Return the number of good starting indices.

### Examples

```
Input: arr = [10,13,12,14,15]
Output: 2
Explanation:
From starting index i = 0, we can make our 1st jump to i = 2 (since arr[2] is the smallest among arr[1], arr[2], arr[3], arr[4] that is greater or equal to arr[0]), then we cannot jump any more.
From starting index i = 1 and i = 2, we can make our 1st jump to i = 3, then we cannot jump any more.
From starting index i = 3, we can make our 1st jump to i = 4, so we have reached the end.
From starting index i = 4, we have reached the end already.
In total, there are 2 different starting indices i = 3 and i = 4, where we can reach the end with some number of jumps.
```

```
Input: arr = [2,3,1,1,4]
Output: 3
Explanation:
From starting index i = 0, we make jumps to i = 1, i = 2, i = 3:
During our 1st jump (odd-numbered), we first jump to i = 1 because arr[1] is the smallest value in [arr[1], arr[2], arr[3], arr[4]] that is greater than or equal to arr[0].
During our 2nd jump (even-numbered), we jump from i = 1 to i = 2 because arr[2] is the largest value in [arr[2], arr[3], arr[4]] that is less than or equal to arr[1]. arr[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3.
During our 3rd jump (odd-numbered), we jump from i = 2 to i = 3 because arr[3] is the smallest value in [arr[3], arr[4]] that is greater than or equal to arr[2].
We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good.
In a similar manner, we can deduce that:
From starting index i = 1, we jump to i = 4, so we reach the end.
From starting index i = 2, we jump to i = 3, and then we can't jump anymore.
From starting index i = 3, we jump to i = 4, so we reach the end.
From starting index i = 4, we are already at the end.
In total, there are 3 different starting indices i = 1, i = 3, and i = 4, where we can reach the end with some number of jumps.
```

```
Input: arr = [5,1,3,4,2]
Output: 3
Explanation: We can reach the end from starting indices 1, 2, and 4.
```

### Constraints

* 1 \<= arr.length \<= 2 \* 10^4
* 0 \<= arr\[i] \< 10^5

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n log n) sorting plus linear DP
    # Space: O(n) for the two jump maps
    def odd_even_jumps(self, arr: list[int]) -> int:
        n = len(arr)

        def make_next(indices: list[int]) -> list[int | None]:
            # For each index j, the next index (in sorted order) greater than j:
            # its first jump target, honoring the smallest-index tie-break.
            nxt: list[int | None] = [None] * n
            stack: list[int] = []
            for i in indices:
                while stack and i > stack[-1]:
                    nxt[stack.pop()] = i
                stack.append(i)
            return nxt

        odd_next = make_next(sorted(range(n), key=lambda i: arr[i]))
        even_next = make_next(sorted(range(n), key=lambda i: -arr[i]))

        # higher[i]: a good end is reachable from i when the next jump is odd-numbered
        higher = [False] * n
        lower = [False] * n
        higher[n - 1] = lower[n - 1] = True
        for i in range(n - 2, -1, -1):
            odd_target = odd_next[i]
            if odd_target is not None:
                higher[i] = lower[odd_target]
            even_target = even_next[i]
            if even_target is not None:
                lower[i] = higher[even_target]

        return sum(higher)
```

## Complexity

| Time | Space |
| - | - |
| O(n log n) sorting plus linear DP | O(n) for the two jump maps |

## Tags


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