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.
Input: arr =[6,4,14,6,8,13,9,7,10,6,12], d =2Output: 4Explanation: 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 5is between index 4 and 6 and 13>9.Similarly You cannot jump from index 3 to index 2 or index 1.
Example 2:
1
2
3
Input: arr =[3,3,3,3,3], d =3Output: 1Explanation: You can start at any index. You always cannot jump to any index.
Example 3:
1
2
3
Input: arr =[7,6,5,4,3,2,1], d =1Output: 7Explanation: Start at index 0. You can visit all the indicies.
The problem asks for the maximum number of indices you can visit by jumping left or right up to d steps, but only to strictly lower values and without “jumping over” higher or equal values. The key insight is that from each index, the best you can do is the maximum of all possible valid jumps from that index, plus one (for the current index). Since jumps can overlap, we use memoization to avoid recomputation.
classSolution {
public:int maxJumps(vector<int>& arr, int d) {
int n = arr.size();
vector<int> memo(n, 0);
function<int(int)> dfs = [&](int i) {
if (memo[i]) return memo[i];
int ans =1;
for (int dir =-1; dir <=1; dir +=2) {
for (int j =1; j <= d; ++j) {
int ni = i + dir * j;
if (ni <0|| ni >= n || arr[ni] >= arr[i]) break;
ans = max(ans, 1+ dfs(ni));
}
}
return memo[i] = ans;
};
int res =0;
for (int i =0; i < n; ++i) res = max(res, dfs(i));
return res;
}
};
classSolution {
publicintmaxJumps(int[] arr, int d) {
int n = arr.length;
int[] memo =newint[n];
return java.util.stream.IntStream.range(0, n)
.map(i -> dfs(arr, d, i, memo))
.max().getAsInt();
}
privateintdfs(int[] arr, int d, int i, int[] memo) {
if (memo[i]!= 0) return memo[i];
int ans = 1, n = arr.length;
for (int dir =-1; dir <= 1; dir += 2) {
for (int j = 1; j <= d; ++j) {
int ni = i + dir * j;
if (ni < 0 || ni >= n || arr[ni]>= arr[i]) break;
ans = Math.max(ans, 1 + dfs(arr, d, ni, memo));
}
}
return memo[i]= ans;
}
}
classSolution {
funmaxJumps(arr: IntArray, d: Int): Int {
val n = arr.size
val memo = IntArray(n)
fundfs(i: Int): Int {
if (memo[i] !=0) return memo[i]
var ans = 1for (dir in listOf(-1, 1)) {
for (j in1..d) {
val ni = i + dir * j
if (ni !in0 until n || arr[ni] >= arr[i]) break ans = maxOf(ans, 1 + dfs(ni))
}
}
memo[i] = ans
return ans
}
return (0 until n).maxOf { dfs(it) }
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution:
defmaxJumps(self, arr: list[int], d: int) -> int:
n: int = len(arr)
memo: list[int] = [0] * n
defdfs(i: int) -> int:
if memo[i]:
return memo[i]
ans: int =1for dir in (-1, 1):
for j in range(1, d +1):
ni: int = i + dir * j
if ni <0or ni >= n or arr[ni] >= arr[i]:
break ans = max(ans, 1+ dfs(ni))
memo[i] = ans
return ans
return max(dfs(i) for i in range(n))