-
Notifications
You must be signed in to change notification settings - Fork 0
/
1340.py
32 lines (30 loc) · 1.11 KB
/
1340.py
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
class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
check_flag = [0] * len(arr)
flag = 0
dp = [1]*len(arr)
while flag < len(arr):
for i in range(len(arr)):
if check_flag[i] == 1:
continue
left = max(0,i-d)
right = min(len(arr)-1 , i+d)
leap_able = 0
for j in range(i-1,left-1,-1):
if arr[j]>= arr[i]:
break
if leap_able == 0 and check_flag[j] == 0:
leap_able = 1
break
dp[i] = max(dp[i],1+dp[j])
for j in range(i+1,right+1):
if arr[j]>= arr[i]:
break
if leap_able == 0 and check_flag[j] == 0:
leap_able = 1
break
dp[i] = max(dp[i],1+dp[j])
if leap_able == 0:
check_flag[i] = 1
flag = flag + 1
return max(dp)