1340. Jump Game V Hard
1/**
2 * [1340] Jump Game V
3 *
4 * Given an array of integers arr and an integer d. In one step you can jump from index i to index:
5 *
6 * i + x where: i + x < arr.length and 0 < x <= d.
7 * i - x where: i - x >= 0 and 0 < x <= d.
8 *
9 * 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)).
10 * You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.
11 * Notice that you can not jump outside of the array at any time.
12 *
13 * Example 1:
14 * <img alt="" src="https://assets.leetcode.com/uploads/2020/01/23/meta-chart.jpeg" style="width: 633px; height: 419px;" />
15 * Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
16 * Output: 4
17 * Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
18 * 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.
19 * Similarly You cannot jump from index 3 to index 2 or index 1.
20 *
21 * Example 2:
22 *
23 * Input: arr = [3,3,3,3,3], d = 3
24 * Output: 1
25 * Explanation: You can start at any index. You always cannot jump to any index.
26 *
27 * Example 3:
28 *
29 * Input: arr = [7,6,5,4,3,2,1], d = 1
30 * Output: 7
31 * Explanation: Start at index 0. You can visit all the indicies.
32 *
33 *
34 * Constraints:
35 *
36 * 1 <= arr.length <= 1000
37 * 1 <= arr[i] <= 10^5
38 * 1 <= d <= arr.length
39 *
40 */
41pub struct Solution {}
42
43// problem: https://leetcode.com/problems/jump-game-v/
44// discuss: https://leetcode.com/problems/jump-game-v/discuss/?currentPage=1&orderBy=most_votes&query=
45
46// submission codes start here
47
48impl Solution {
49 pub fn max_jumps(arr: Vec<i32>, d: i32) -> i32 {
50 0
51 }
52}
53
54// submission codes end
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59
60 #[test]
61 fn test_1340() {
62 }
63}
64
Back
© 2025 bowen.ge All Rights Reserved.