@@ -47,7 +47,7 @@ We can go from 2 → 14 → 12 with the following 2 operations.
47
47
<strong >Input:</strong > nums = [3,5,7], start = 0, goal = -4
48
48
<strong >Output:</strong > 2
49
49
<strong >Explanation:</strong >
50
- We can go from 0 &rarr ; 3 &rarr ; -4 with the following 2 operations.
50
+ We can go from 0 &rarr ; 3 &rarr ; -4 with the following 2 operations.
51
51
- 0 + 3 = 3
52
52
- 3 - 7 = -4
53
53
Note that the last operation sets x out of the range 0 < ; = x < ; = 1000, which is valid.
@@ -66,9 +66,9 @@ There is no way to convert 0 into 1.</pre>
66
66
<pre >
67
67
<strong >Input:</strong > nums = [1], start = 0, goal = 3
68
68
<strong >Output:</strong > 3
69
- <strong >Explanation:</strong >
70
- We can go from 0 &rarr ; 1 &rarr ; 2 &rarr ; 3 with the following 3 operations.
71
- - 0 + 1 = 1
69
+ <strong >Explanation:</strong >
70
+ We can go from 0 &rarr ; 1 &rarr ; 2 &rarr ; 3 with the following 3 operations.
71
+ - 0 + 1 = 1
72
72
- 1 + 1 = 2
73
73
- 2 + 1 = 3
74
74
</pre >
93
93
### ** Python3**
94
94
95
95
``` python
96
-
96
+ class Solution :
97
+ def minimumOperations (self , nums : List[int ], start : int , goal : int ) -> int :
98
+ op1 = lambda x , y : x + y
99
+ op2 = lambda x , y : x - y
100
+ op3 = lambda x , y : x ^ y
101
+ ops = [op1, op2, op3]
102
+ vis = [False ] * 1001
103
+ q = deque([(start, 0 )])
104
+ while q:
105
+ x, step = q.popleft()
106
+ for num in nums:
107
+ for op in ops:
108
+ nx = op(x, num)
109
+ if nx == goal:
110
+ return step + 1
111
+ if nx >= 0 and nx <= 1000 and not vis[nx]:
112
+ q.append((nx, step + 1 ))
113
+ vis[nx] = True
114
+ return - 1
97
115
```
98
116
99
117
### ** Java**
100
118
101
119
``` java
120
+ class Solution {
121
+ public int minimumOperations (int [] nums , int start , int goal ) {
122
+ IntBinaryOperator op1 = (x, y) - > x + y;
123
+ IntBinaryOperator op2 = (x, y) - > x - y;
124
+ IntBinaryOperator op3 = (x, y) - > x ^ y;
125
+ IntBinaryOperator [] ops = { op1, op2, op3 };
126
+ boolean [] vis = new boolean [1001 ];
127
+ Queue<int[]> queue = new ArrayDeque<> ();
128
+ queue. offer(new int [] { start, 0 });
129
+ while (! queue. isEmpty()) {
130
+ int [] p = queue. poll();
131
+ int x = p[0 ], step = p[1 ];
132
+ for (int num : nums) {
133
+ for (IntBinaryOperator op : ops) {
134
+ int nx = op. applyAsInt(x, num);
135
+ if (nx == goal) {
136
+ return step + 1 ;
137
+ }
138
+ if (nx >= 0 && nx <= 1000 && ! vis[nx]) {
139
+ queue. offer(new int [] { nx, step + 1 });
140
+ vis[nx] = true ;
141
+ }
142
+ }
143
+ }
144
+ }
145
+ return - 1 ;
146
+ }
147
+ }
148
+ ```
149
+
150
+ ### ** C++**
151
+
152
+ ``` cpp
153
+ class Solution {
154
+ public:
155
+ int minimumOperations(vector<int >& nums, int start, int goal) {
156
+ using pii = pair<int, int>;
157
+ vector<function<int(int, int)>> ops{
158
+ [ ] (int x, int y) { return x + y; },
159
+ [ ] (int x, int y) { return x - y; },
160
+ [ ] (int x, int y) { return x ^ y; },
161
+ };
162
+ vector<bool > vis(1001, false);
163
+ queue<pii > q;
164
+ q.push({start, 0});
165
+ while (!q.empty()) {
166
+ auto [ x, step] = q.front();
167
+ q.pop();
168
+ for (int num : nums) {
169
+ for (auto op : ops) {
170
+ int nx = op(x, num);
171
+ if (nx == goal) {
172
+ return step + 1;
173
+ }
174
+ if (nx >= 0 && nx <= 1000 && !vis[ nx] ) {
175
+ q.push({nx, step + 1});
176
+ vis[ nx] = true;
177
+ }
178
+ }
179
+ }
180
+ }
181
+ return -1;
182
+ }
183
+ };
184
+ ```
102
185
186
+ ### **Go**
187
+
188
+ ```go
189
+ func minimumOperations(nums []int, start int, goal int) int {
190
+ type pair struct {
191
+ x int
192
+ step int
193
+ }
194
+
195
+ ops := []func(int, int) int{
196
+ func(x, y int) int { return x + y },
197
+ func(x, y int) int { return x - y },
198
+ func(x, y int) int { return x ^ y },
199
+ }
200
+ vis := make([]bool, 1001)
201
+ q := []pair{{start, 0}}
202
+
203
+ for len(q) > 0 {
204
+ x, step := q[0].x, q[0].step
205
+ q = q[1:]
206
+ for _, num := range nums {
207
+ for _, op := range ops {
208
+ nx := op(x, num)
209
+ if nx == goal {
210
+ return step + 1
211
+ }
212
+ if nx >= 0 && nx <= 1000 && !vis[nx] {
213
+ q = append(q, pair{nx, step + 1})
214
+ vis[nx] = true
215
+ }
216
+ }
217
+ }
218
+ }
219
+ return -1
220
+ }
103
221
```
104
222
105
223
### ** TypeScript**
0 commit comments