1654. Minimum Jumps to Reach Home Medium

@problem@discussion
#Array#Dynamic Programming#Breadth-First Search



1/**
2 * [1654] Minimum Jumps to Reach Home
3 *
4 * A certain bug's home is on the x-axis at position x. Help them get there from position 0.
5 * The bug jumps according to the following rules:
6 * 
7 * 	It can jump exactly a positions forward (to the right).
8 * 	It can jump exactly b positions backward (to the left).
9 * 	It cannot jump backward twice in a row.
10 * 	It cannot jump to any forbidden positions.
11 * 
12 * The bug may jump forward beyond its home, but it cannot jump to positions numbered with negative integers.
13 * Given an array of integers forbidden, where forbidden[i] means that the bug cannot jump to the position forbidden[i], and integers a, b, and x, return the minimum number of jumps needed for the bug to reach its home. If there is no possible sequence of jumps that lands the bug on position x, return -1.
14 *  
15 * Example 1:
16 * 
17 * Input: forbidden = [14,4,18,1,15], a = 3, b = 15, x = 9
18 * Output: 3
19 * Explanation: 3 jumps forward (0 -> 3 -> 6 -> 9) will get the bug home.
20 * 
21 * Example 2:
22 * 
23 * Input: forbidden = [8,3,16,6,12,20], a = 15, b = 13, x = 11
24 * Output: -1
25 * 
26 * Example 3:
27 * 
28 * Input: forbidden = [1,6,2,14,5,17,4], a = 16, b = 9, x = 7
29 * Output: 2
30 * Explanation: One jump forward (0 -> 16) then one jump backward (16 -> 7) will get the bug home.
31 * 
32 *  
33 * Constraints:
34 * 
35 * 	1 <= forbidden.length <= 1000
36 * 	1 <= a, b, forbidden[i] <= 2000
37 * 	0 <= x <= 2000
38 * 	All the elements in forbidden are distinct.
39 * 	Position x is not forbidden.
40 * 
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/minimum-jumps-to-reach-home/
45// discuss: https://leetcode.com/problems/minimum-jumps-to-reach-home/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50    pub fn minimum_jumps(forbidden: Vec<i32>, a: i32, b: i32, x: i32) -> i32 {
51        0
52    }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_1654() {
63    }
64}
65


Back
© 2025 bowen.ge All Rights Reserved.