Leetcode #975: Odd Even Jump
In this guide, we solve Leetcode #975 Odd Even Jump in Python and focus on the core idea that makes the solution efficient.
You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Problem Statement
You are given an integer array arr. From some starting index, you can make a series of jumps.
Quick Facts
- Difficulty: Hard
- Premium: No
- Tags: Stack, Array, Dynamic Programming, Ordered Set, Sorting, Monotonic Stack
Intuition
The problem breaks into overlapping subproblems, so caching results prevents exponential repetition.
A carefully chosen DP state captures exactly what we need to build the final answer.
Approach
Define the DP state and recurrence, then compute states in the correct order.
Optionally compress space once the recurrence is clear.
Steps:
- Choose a DP state definition.
- Write the recurrence and base cases.
- Compute states in the correct order.
Example
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.
Python Solution
class Solution:
def oddEvenJumps(self, arr: List[int]) -> int:
def dfs(i: int, k: int) -> bool:
if i == n - 1:
return True
if g[i][k] == -1:
return False
return dfs(g[i][k], k ^ 1)
n = len(arr)
g = [[0] * 2 for _ in range(n)]
sd = SortedDict()
for i in range(n - 1, -1, -1):
j = sd.bisect_left(arr[i])
g[i][1] = sd.values()[j] if j < len(sd) else -1
j = sd.bisect_right(arr[i]) - 1
g[i][0] = sd.values()[j] if j >= 0 else -1
sd[arr[i]] = i
return sum(dfs(i, 1) for i in range(n))
Complexity
The time complexity is , and the space complexity is . The space complexity is .
Edge Cases and Pitfalls
Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.
Summary
This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.