-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjumpGameV.py
More file actions
27 lines (24 loc) · 796 Bytes
/
jumpGameV.py
File metadata and controls
27 lines (24 loc) · 796 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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/jump-game-v/
# Author: Miao Zhang
# Date: 2021-04-25
class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
n = len(arr)
memo = [[] for _ in range(n)]
for i in range(len(arr)):
memo[i].append(arr[i])
memo[i].append(i)
memo.sort()
dp = [1 for _ in range(n)]
for v, i in memo:
j = i + 1
while j < min(n, i + d + 1) and arr[i] > arr[j]:
dp[i] = max(dp[i], dp[j] + 1)
j += 1
j = i - 1
while j > max(-1, i - d - 1) and arr[i] > arr[j]:
dp[i] = max(dp[i], dp[j] + 1)
j -= 1
return max(dp)