Given an array of integers arr and an integer d. In one step you can jump from index i to index:
i + x where: i + x < arr.length and 0 < x <= d.
i - x where: i - x >= 0 and 0 < x <= d.
In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).
You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.
Notice that you can not jump outside of the array at any time.
Example 1:
Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
Output: 4
Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9.
Similarly You cannot jump from index 3 to index 2 or index 1.
Example 2:
Input: arr = [3,3,3,3,3], d = 3
Output: 1
Explanation: You can start at any index. You always cannot jump to any index.
Example 3:
Input: arr = [7,6,5,4,3,2,1], d = 1
Output: 7
Explanation: Start at index 0. You can visit all the indicies.
Constraints:
1 <= arr.length <= 1000
1 <= arr[i] <= 105
1 <= d <= arr.length
Solutions
Solution 1: Memoized Search
Thinking
From \(i\) we may jump at most \(d\) away to a strictly shorter index with nothing taller in between; we want the longest visit from any start. \(n \le 1000\) allows memoized \(dfs(i)\): scan left and right until we leave the window or meet a bar that is not shorter, recurse, and add one. The answer is the maximum over starts.
We design a function \(\text{dfs}(i)\) to represent the maximum number of indices that can be visited starting from index \(i\). We enumerate all valid jump targets \(j\) for \(i\), where \(i - d \leq j \leq i + d\) and \(\text{arr}[i] > \text{arr}[j]\). For each valid \(j\), we recursively compute \(\text{dfs}(j)\) and take the maximum among them. The final answer is the maximum value of \(\text{dfs}(i)\) over all indices \(i\).
We can use memoized search to optimize this process, that is, use an array \(f\) to record the value of \(\text{dfs}\) for each index and avoid repeated computation.
The time complexity is \(O(n \times d)\), and the space complexity is \(O(n)\), where \(n\) is the length of the array \(\text{arr}\).
Solution 1 already fills this table from shorter bars to taller ones. The transition below is that same table.
We can pair each element \(x\) in the array \(\text{arr}\) with its index \(i\) to form a tuple \((x, i)\), and sort these tuples in ascending order by \(x\).
Next, define \(f[i]\) to be the maximum number of indices that can be visited starting from index \(i\). Initially, \(f[i] = 1\), meaning each index can be visited alone as a single jump.
We enumerate \(i\) in the order of the tuples \((x, i)\), and enumerate all valid jump targets \(j\) for \(i\), namely \(i - d \leq j \leq i + d\), with \(\text{arr}[i] > \text{arr}[j]\). For each valid \(j\), we can update \(f[i]\), namely \(f[i] = \max(f[i], 1 + f[j])\).
The final answer is \(\max_{0 \leq i < n} f[i]\).
The time complexity is \(O(n \log n + n \times d)\), and the space complexity is \(O(n)\). Here, \(n\) is the length of the array \(\text{arr}\).
Expanding every jump onto a shorter bar is exponential. The array length can reach \(1000\), and the first call from a bar always lands on the next shorter neighbor, so the chain has depth \(n\). A jump lands only on a strictly shorter index, and every index in between must also be strictly shorter, so \(f[i]\) depends only on shorter indices. Sorting the indices by height makes every legal \(j\) already final when \(i\) is filled, and the update is \(f[i] = \max(f[i], 1 + f[j])\) with each entry starting at \(1\). The answer is the largest entry.
We let \(f[i]\) be the maximum number of indices that can be visited starting from index \(i\), and set \(f[i] = 1\) initially.
Sort the indices in increasing order of \(\text{arr}[i]\). When index \(i\) is processed, scan left and right until the distance exceeds \(d\) or a bar at least as tall as \(\text{arr}[i]\) appears. For each legal index \(j\) on that scan, update \(f[i] = \max(f[i], 1 + f[j])\). Shorter bars have already been processed, so \(f[j]\) is final.
The answer is the maximum value of \(f[i]\).
The time complexity is \(O(n \log n + n \times d)\), and the space complexity is \(O(n)\), where \(n\) is the length of \(\text{arr}\).