Formatted question description: https://leetcode.ca/all/1340.html

1340. Jump Game V (Hard)

Given an array of integers arr and an integer d. In one step you can jump from index i to index:

  • i + x where: i + x < arr.length and 0 < x <= d.
  • i - x where: i - x >= 0 and 0 < x <= d.

In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).

You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.

Notice that you can not jump outside of the array at any time.

 

Example 1:

Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
Output: 4
Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9.
Similarly You cannot jump from index 3 to index 2 or index 1.

Example 2:

Input: arr = [3,3,3,3,3], d = 3
Output: 1
Explanation: You can start at any index. You always cannot jump to any index.

Example 3:

Input: arr = [7,6,5,4,3,2,1], d = 1
Output: 7
Explanation: Start at index 0. You can visit all the indicies. 

Example 4:

Input: arr = [7,1,7,1,7,1], d = 2
Output: 2

Example 5:

Input: arr = [66], d = 1
Output: 1

 

Constraints:

  • 1 <= arr.length <= 1000
  • 1 <= arr[i] <= 10^5
  • 1 <= d <= arr.length

Related Topics:
Dynamic Programming

Solution 1. DFS + Memo

// OJ: https://leetcode.com/problems/jump-game-v/
// Time: O(ND)
// Space: O(N)
class Solution {
    int N, D;
    vector<int> v;
    int dfs(vector<int>& arr, int start) {
        if (v[start] > 0) return v[start];
        int left = 0, right = 0;
        for (int d = 1; d <= D && start - d >= 0 && arr[start - d] < arr[start]; ++d)
            left = max(left, dfs(arr, start - d));
        for (int d = 1; d <= D && start + d < N && arr[start + d] < arr[start]; ++d)
            right = max(right, dfs(arr, start + d));
        return v[start] = 1 + max(left, right);
    }
public:
    int maxJumps(vector<int>& arr, int d) {
        N = arr.size(), D = d;
        v = vector<int>(N, 0);
        for (int i = 0; i < N; ++i) dfs(arr, i);
        int ans = 0;
        for (int i = 0; i < N; ++i) ans = max(ans, v[i]);
        return ans;
    }
};

Or

// OJ: https://leetcode.com/problems/jump-game-v/
// Time: O(ND)
// Space: O(N)
class Solution {
    vector<int> m;
    int N;
    int dp(vector<int> &A, int d, int i) {
        if (m[i]) return m[i];
        int ans = 1;
        for (int k = 0; k < 2; ++k) {
            int maxVal = INT_MIN;
            for (int j = 1; j <= d; ++j) {
                int t = k == 0 ? i + j : i - j;
                if (t >= N || t < 0 || A[t] >= A[i]) break;
                maxVal = max(maxVal, A[t]);
                if (A[t] != maxVal) continue;
                ans = max(ans, 1 + dp(A, d, t));
            }
        }
        return m[i] = ans;
    }
public:
    int maxJumps(vector<int>& A, int d) {
        N = A.size();
        m.assign(N, 0);
        int ans = 0;
        for (int i = 0; i < N; ++i) ans = max(ans, dp(A, d, i));
        return ans;
    }
};

Solution 2. DP + Monotonous Stack

// OJ: https://leetcode.com/problems/jump-game-v/
// Time: O(N)
// Space: O(N)
class Solution {
public:
    int maxJumps(vector<int>& A, int d) {
        int N = A.size();
        vector<int> dp(N + 1, 1), left, right;
        A.push_back(INT_MAX);
        for (int i = 0; i <= N; ++i) {
            while (left.size() && A[left.back()] < A[i]) {
                int pre = A[left.back()];
                while (left.size() && A[left.back()] == pre) {
                    int j = left.back();
                    left.pop_back();
                    if (i - j <= d) dp[i] = max(dp[i], dp[j] + 1);
                    right.push_back(j);
                }
                while (right.size()) {
                    int j = right.back();
                    right.pop_back();
                    if (left.size() && j - left.back() <= d) dp[left.back()] = max(dp[left.back()], dp[j] + 1);
                }
            }
            left.push_back(i);
        }
        return *max_element(dp.begin(), dp.end() - 1);
    }
};

Java

  • class Solution {
        public int maxJumps(int[] arr, int d) {
            int length = arr.length;
            int[][] indexHeightArray = new int[length][2];
            for (int i = 0; i < length; i++) {
                indexHeightArray[i][0] = i;
                indexHeightArray[i][1] = arr[i];
            }
            Arrays.sort(indexHeightArray, new Comparator<int[]>() {
                public int compare(int[] indexHeight1, int[] indexHeight2) {
                    return indexHeight1[1] - indexHeight2[1];
                }
            });
            int[] dp = new int[length];
            Arrays.fill(dp, 1);
            for (int i = 0; i < length; i++) {
                int[] indexHeight = indexHeightArray[i];
                int startIndex = indexHeight[0];
                int curHeight = indexHeight[1];
                int leftMost = Math.max(startIndex - d, 0);
                int rightMost = Math.min(startIndex + d, length - 1);
                for (int j = startIndex - 1; j >= leftMost; j--) {
                    if (arr[j] >= curHeight)
                        break;
                    dp[startIndex] = Math.max(dp[startIndex], dp[j] + 1);
                }
                for (int j = startIndex + 1; j <= rightMost; j++) {
                    if (arr[j] >= curHeight)
                        break;
                    dp[startIndex] = Math.max(dp[startIndex], dp[j] + 1);
                }
            }
            int maxJump = 0;
            for (int jump : dp)
                maxJump = Math.max(maxJump, jump);
            return maxJump;
        }
    }
    
  • // OJ: https://leetcode.com/problems/jump-game-v/
    // Time: O(ND)
    // Space: O(N)
    class Solution {
        int N, D;
        vector<int> v;
        int dfs(vector<int>& arr, int start) {
            if (v[start] > 0) return v[start];
            int left = 0, right = 0;
            for (int d = 1; d <= D && start - d >= 0 && arr[start - d] < arr[start]; ++d)
                left = max(left, dfs(arr, start - d));
            for (int d = 1; d <= D && start + d < N && arr[start + d] < arr[start]; ++d)
                right = max(right, dfs(arr, start + d));
            return v[start] = 1 + max(left, right);
        }
    public:
        int maxJumps(vector<int>& arr, int d) {
            N = arr.size(), D = d;
            v = vector<int>(N, 0);
            for (int i = 0; i < N; ++i) dfs(arr, i);
            int ans = 0;
            for (int i = 0; i < N; ++i) ans = max(ans, v[i]);
            return ans;
        }
    };
    
  • print("Todo!")
    

All Problems

All Solutions