-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathJumpGame5.java
35 lines (31 loc) · 955 Bytes
/
JumpGame5.java
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
35
/*https://leetcode.com/problems/jump-game-v/*/
class Solution {
int N;
Integer[] store;
public int maxJumps(int[] arr, int d) {
int result = 0, i;
N = arr.length;
store = new Integer[N];
for (i = 0; i < N; ++i)
result = Math.max(result,count(arr,d,i)+1);
return result;
}
private int count(int[] arr, int d, int index)
{
if (store[index] != null) return store[index];
int result = 0;
for (int i = index-1; i >= Math.max(index-d,0); --i)
{
if (arr[i] < arr[index])
result = Math.max(count(arr,d,i)+1,result);
else break;
}
for (int i = index+1; i <= Math.min(index+d,N-1); ++i)
{
if (arr[i] < arr[index])
result = Math.max(count(arr,d,i)+1,result);
else break;
}
return store[index] = result;
}
}