Skip to content

Commit 5ff28ef

Browse files
Added task 2059.
1 parent 08e8dac commit 5ff28ef

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g2001_2100.s2059_minimum_operations_to_convert_number;
2+
3+
// #Medium #Array #Breadth_First_Search #2022_05_28_Time_97_ms_(64.14%)_Space_116.9_MB_(51.03%)
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
public class Solution {
10+
public int minimumOperations(int[] nums, int start, int goal) {
11+
boolean[] seen = new boolean[1001];
12+
List<Integer> q = Arrays.asList(goal);
13+
int cnt = 0;
14+
while (!q.isEmpty()) {
15+
++cnt;
16+
List<Integer> q1 = new ArrayList<>();
17+
for (int x : q) {
18+
for (int n : nums) {
19+
for (int xn : new int[] {x + n, x - n, x ^ n}) {
20+
if (xn >= 0 && xn <= 1000 && !seen[xn]) {
21+
if (xn == start) {
22+
return cnt;
23+
}
24+
seen[xn] = true;
25+
q1.add(xn);
26+
}
27+
}
28+
}
29+
q = q1;
30+
}
31+
}
32+
return -1;
33+
}
34+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2059\. Minimum Operations to Convert Number
2+
3+
Medium
4+
5+
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`:
6+
7+
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:
8+
9+
* `x + nums[i]`
10+
* `x - nums[i]`
11+
* `x ^ nums[i]` (bitwise-XOR)
12+
13+
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.
14+
15+
Return _the **minimum** number of operations needed to convert_ `x = start` _into_ `goal`_, and_ `-1` _if it is not possible_.
16+
17+
**Example 1:**
18+
19+
**Input:** nums = [2,4,12], start = 2, goal = 12
20+
21+
**Output:** 2
22+
23+
**Explanation:** We can go from 2 → 14 → 12 with the following 2 operations.
24+
- 2 + 12 = 14
25+
- 14 - 2 = 12
26+
27+
**Example 2:**
28+
29+
**Input:** nums = [3,5,7], start = 0, goal = -4
30+
31+
**Output:** 2
32+
33+
**Explanation:** We can go from 0 → 3 → -4 with the following 2 operations.
34+
- 0 + 3 = 3
35+
- 3 - 7 = -4
36+
37+
Note that the last operation sets x out of the range 0 <= x <= 1000, which is valid.
38+
39+
**Example 3:**
40+
41+
**Input:** nums = [2,8,16], start = 0, goal = 1
42+
43+
**Output:** -1
44+
45+
**Explanation:**
46+
There is no way to convert 0 into 1.
47+
48+
**Constraints:**
49+
50+
* `1 <= nums.length <= 1000`
51+
* <code>-10<sup>9</sup> <= nums[i], goal <= 10<sup>9</sup></code>
52+
* `0 <= start <= 1000`
53+
* `start != goal`
54+
* All the integers in `nums` are distinct.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g2001_2100.s2059_minimum_operations_to_convert_number;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void minimumOperations() {
11+
assertThat(new Solution().minimumOperations(new int[] {2, 4, 12}, 2, 12), equalTo(2));
12+
}
13+
14+
@Test
15+
void minimumOperations2() {
16+
assertThat(new Solution().minimumOperations(new int[] {3, 5, 7}, 0, -4), equalTo(2));
17+
}
18+
19+
@Test
20+
void minimumOperations3() {
21+
assertThat(new Solution().minimumOperations(new int[] {2, 8, 16}, 0, 1), equalTo(-1));
22+
}
23+
}

0 commit comments

Comments
 (0)