Class Solution

java.lang.Object
g1301_1400.s1340_jump_game_v.Solution

public class Solution extends Object
1340 - Jump Game V.<p>Hard</p> <p>Given an array of integers <code>arr</code> and an integer <code>d</code>. In one step you can jump from index <code>i</code> to index:</p> <ul> <li><code>i + x</code> where: <code>i + x < arr.length</code> and <code>0 < x <= d</code>.</li> <li><code>i - x</code> where: <code>i - x >= 0</code> and <code>0 < x <= d</code>.</li> </ul> <p>In addition, you can only jump from index <code>i</code> to index <code>j</code> if <code>arr[i] > arr[j]</code> and <code>arr[i] > arr[k]</code> for all indices <code>k</code> between <code>i</code> and <code>j</code> (More formally <code>min(i, j) < k < max(i, j)</code>).</p> <p>You can choose any index of the array and start jumping. Return <em>the maximum number of indices</em> you can visit.</p> <p>Notice that you can not jump outside of the array at any time.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2020/01/23/meta-chart.jpeg" alt="" /></p> <p><strong>Input:</strong> arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong> You can start at index 10. You can jump 10 &ndash;> 8 &ndash;> 6 &ndash;> 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.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> arr = [3,3,3,3,3], d = 3</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong> You can start at any index. You always cannot jump to any index.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> arr = [7,6,5,4,3,2,1], d = 1</p> <p><strong>Output:</strong> 7</p> <p><strong>Explanation:</strong> Start at index 0. You can visit all the indicies.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= arr.length <= 1000</code></li> <li><code>1 <= arr[i] <= 10<sup>5</sup></code></li> <li><code>1 <= d <= arr.length</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • maxJumps

      public int maxJumps(int[] arr, int d)