-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy path1340. Jump Game V
More file actions
34 lines (27 loc) · 908 Bytes
/
1340. Jump Game V
File metadata and controls
34 lines (27 loc) · 908 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution {
int[] dp;
public int maxJumps(int[] arr, int d) {
int max = 0;
dp = new int[arr.length];
for(int i = 0;i<arr.length;i++){
max = Math.max(max,helper(arr,d,i));
}
return max;
}
private int helper(int[] arr,int d, int index){
if(dp[index]>0){
return dp[index];
}
int result = 1;
//index - 1 ----- index - d
for(int j = index - 1; j>= Math.max(index - d,0) && arr[index]>arr[j]; j--){
result = Math.max(result, 1 + helper(arr,d,j));
}
//index + 1 ------- index + d
for(int j = index + 1; j<= Math.min(index + d,arr.length-1) && arr[index]>arr[j] ; j++){
result = Math.max(result, 1 + helper(arr,d,j));
}
dp[index] = result;
return result;
}
}