Skip to content

Commit 7ee9c54

Browse files
authored
Create 1340. Jump Game V
1 parent d91d35f commit 7ee9c54

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

1340. Jump Game V

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
3+
int[] dp;
4+
public int maxJumps(int[] arr, int d) {
5+
int max = 0;
6+
dp = new int[arr.length];
7+
for(int i = 0;i<arr.length;i++){
8+
max = Math.max(max,helper(arr,d,i));
9+
}
10+
11+
return max;
12+
}
13+
14+
private int helper(int[] arr,int d, int index){
15+
16+
if(dp[index]>0){
17+
return dp[index];
18+
}
19+
20+
int result = 1;
21+
//index - 1 ----- index - d
22+
for(int j = index - 1; j>= Math.max(index - d,0) && arr[index]>arr[j]; j--){
23+
result = Math.max(result, 1 + helper(arr,d,j));
24+
}
25+
26+
//index + 1 ------- index + d
27+
for(int j = index + 1; j<= Math.min(index + d,arr.length-1) && arr[index]>arr[j] ; j++){
28+
result = Math.max(result, 1 + helper(arr,d,j));
29+
}
30+
31+
dp[index] = result;
32+
return result;
33+
}
34+
}

0 commit comments

Comments
 (0)