Problem Description
Given an integer array arr, you can jump from a starting index by performing a series of jumps. On odd-numbered jumps, you jump to the index j (with i < j) such that arr[i] <= arr[j] and arr[j] is the smallest possible value reachable; if multiple candidates exist, pick the smallest index. On even-numbered jumps, you jump to the index j (with i < j) such that arr[i] >= arr[j] and arr[j] is the largest possible value reachable; if multiple candidates exist, pick the smallest index. An index is "good" if starting from it you can eventually reach the last index. Return the number of good starting indices.
Key Insights
- Use dynamic programming to determine if you can reach the end from each index for odd and even jumps.
- Precompute the next jump index for odd and even jumps using a monotonic stack technique.
- Sort indices by value (and index to break ties) for odd jumps and sort by descending order for even jumps.
- Use two DP arrays: one represents if we can reach the end starting with an odd jump, another for even jumps.
- The answer is the count of indices where starting with an odd jump (the first jump) leads to the end.
Space and Time Complexity
Time Complexity: O(n log n) due to sorting the indices. Space Complexity: O(n) for the DP arrays and precomputed jump arrays.
Solution
We start by precomputing the next jump destination for both odd and even jumps using a monotonic stack approach. For odd jumps, sort the indices in increasing order based on arr values (and index in case of a tie) then use a stack to compute the nearest higher or equal index. For even jumps, do a similar procedure but sort indices in decreasing order to compute the next lower or equal index.
Once we have these jump targets, we fill two DP arrays: dpOdd and dpEven. dpOdd[i] indicates that starting from index i with an odd jump can reach the end, while dpEven[i] serves the even jump case. We know the last index is always reachable, giving dpOdd[n - 1] = dpEven[n - 1] = true. We then iterate backwards from the penultimate index to the start, setting:
- dpOdd[i] = dpEven[nextHigher[i]] if a valid next index exists.
- dpEven[i] = dpOdd[nextLower[i]] if a valid next index exists. Finally, the answer is the count of indices i for which dpOdd[i] is true since the first jump is odd.