Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

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.

Leetcode

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: @cache 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 O(n×log⁡n)O(n \times \log n)O(n×logn), and the space complexity is O(n)O(n)O(n). The space complexity is O(n)O(n)O(n).

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.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy