File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments