2059. Minimum Operations to Convert Number Medium

@problem@discussion
#Array#Breadth-First Search



1/**
2 * [2059] Minimum Operations to Convert Number
3 *
4 * You are given a 0-indexed integer array nums containing distinct numbers, an integer start, and an integer goal. There is an integer x that is initially set to start, and you want to perform operations on x such that it is converted to goal. You can perform the following operation repeatedly on the number x:
5 * If 0 <= x <= 1000, then for any index i in the array (0 <= i < nums.length), you can set x to any of the following:
6 * 
7 * 	x + nums[i]
8 * 	x - nums[i]
9 * 	x ^ nums[i] (bitwise-XOR)
10 * 
11 * Note that you can use each nums[i] any number of times in any order. Operations that set x to be out of the range 0 <= x <= 1000 are valid, but no more operations can be done afterward.
12 * Return the minimum number of operations needed to convert x = start into goal, and -1 if it is not possible.
13 *  
14 * Example 1:
15 * 
16 * Input: nums = [2,4,12], start = 2, goal = 12
17 * Output: 2
18 * Explanation: We can go from 2 &rarr; 14 &rarr; 12 with the following 2 operations.
19 * - 2 + 12 = 14
20 * - 14 - 2 = 12
21 * 
22 * Example 2:
23 * 
24 * Input: nums = [3,5,7], start = 0, goal = -4
25 * Output: 2
26 * Explanation: We can go from 0 &rarr; 3 &rarr; -4 with the following 2 operations. 
27 * - 0 + 3 = 3
28 * - 3 - 7 = -4
29 * Note that the last operation sets x out of the range 0 <= x <= 1000, which is valid.
30 * 
31 * Example 3:
32 * 
33 * Input: nums = [2,8,16], start = 0, goal = 1
34 * Output: -1
35 * Explanation: There is no way to convert 0 into 1.
36 * 
37 *  
38 * Constraints:
39 * 
40 * 	1 <= nums.length <= 1000
41 * 	-10^9 <= nums[i], goal <= 10^9
42 * 	0 <= start <= 1000
43 * 	start != goal
44 * 	All the integers in nums are distinct.
45 * 
46 */
47pub struct Solution {}
48
49// problem: https://leetcode.com/problems/minimum-operations-to-convert-number/
50// discuss: https://leetcode.com/problems/minimum-operations-to-convert-number/discuss/?currentPage=1&orderBy=most_votes&query=
51
52// submission codes start here
53
54impl Solution {
55    pub fn minimum_operations(nums: Vec<i32>, start: i32, goal: i32) -> i32 {
56        0
57    }
58}
59
60// submission codes end
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn test_2059() {
68    }
69}
70


Back
© 2025 bowen.ge All Rights Reserved.