Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e8fc580

Browse files
feat: add solution to lc problem: No. 3377 (doocs#3933)
1 parent bfb00b9 commit e8fc580

File tree

6 files changed

+814
-8
lines changed

6 files changed

+814
-8
lines changed

‎solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/README.md‎

Lines changed: 271 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,25 +105,292 @@ tags:
105105
#### Python3
106106

107107
```python
108-
108+
import heapq
109+
110+
class Solution:
111+
def __init__(self):
112+
self.sieve = []
113+
114+
def run_sieve(self):
115+
self.sieve = [True] * 100000
116+
self.sieve[0], self.sieve[1] = False, False
117+
for i in range(2, 100000):
118+
if self.sieve[i]:
119+
for j in range(2 * i, 100000, i):
120+
self.sieve[j] = False
121+
122+
def solve(self, n, m):
123+
pq = []
124+
heapq.heappush(pq, (n, n))
125+
visited = set()
126+
127+
while pq:
128+
sum_, cur = heapq.heappop(pq)
129+
130+
if cur in visited:
131+
continue
132+
visited.add(cur)
133+
134+
if cur == m:
135+
return sum_
136+
137+
s = list(str(cur))
138+
for i in range(len(s)):
139+
c = s[i]
140+
141+
if s[i] < '9':
142+
s[i] = chr(ord(s[i]) + 1)
143+
next_ = int(''.join(s))
144+
if not self.sieve[next_] and next_ not in visited:
145+
heapq.heappush(pq, (sum_ + next_, next_))
146+
s[i] = c
147+
148+
if s[i] > '0' and not (i == 0 and s[i] == '1'):
149+
s[i] = chr(ord(s[i]) - 1)
150+
next_ = int(''.join(s))
151+
if not self.sieve[next_] and next_ not in visited:
152+
heapq.heappush(pq, (sum_ + next_, next_))
153+
s[i] = c
154+
155+
return -1
156+
157+
def minOperations(self, n, m):
158+
self.run_sieve()
159+
if self.sieve[n] or self.sieve[m]:
160+
return -1
161+
return self.solve(n, m)
109162
```
110163

111164
#### Java
112165

113166
```java
114-
167+
class Solution {
168+
private boolean[] sieve;
169+
170+
private void runSieve() {
171+
sieve = new boolean[100000];
172+
Arrays.fill(sieve, true);
173+
sieve[0] = false;
174+
sieve[1] = false;
175+
for (int i = 2; i < 100000; i++) {
176+
if (sieve[i]) {
177+
for (int j = 2 * i; j < 100000; j += i) {
178+
sieve[j] = false;
179+
}
180+
}
181+
}
182+
}
183+
184+
private int solve(int n, int m) {
185+
PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
186+
pq.add(new int[]{n, n});
187+
Set<Integer> visited = new HashSet<>();
188+
189+
while (!pq.isEmpty()) {
190+
int[] top = pq.poll();
191+
int sum = top[0], cur = top[1];
192+
193+
if (visited.contains(cur)) {
194+
continue;
195+
}
196+
visited.add(cur);
197+
198+
if (cur == m) {
199+
return sum;
200+
}
201+
202+
char[] s = String.valueOf(cur).toCharArray();
203+
for (int i = 0; i < s.length; i++) {
204+
char c = s[i];
205+
206+
if (s[i] < '9') {
207+
s[i] = (char) (s[i] + 1);
208+
int next = Integer.parseInt(new String(s));
209+
if (!sieve[next] && !visited.contains(next)) {
210+
pq.add(new int[]{sum + next, next});
211+
}
212+
s[i] = c;
213+
}
214+
215+
if (s[i] > '0' && !(i == 0 && s[i] == '1')) {
216+
s[i] = (char) (s[i] - 1);
217+
int next = Integer.parseInt(new String(s));
218+
if (!sieve[next] && !visited.contains(next)) {
219+
pq.add(new int[]{sum + next, next});
220+
}
221+
s[i] = c;
222+
}
223+
}
224+
}
225+
226+
return -1;
227+
}
228+
229+
public int minOperations(int n, int m) {
230+
runSieve();
231+
if (sieve[n] || sieve[m]) {
232+
return -1;
233+
}
234+
return solve(n, m);
235+
}
236+
}
115237
```
116238

117239
#### C++
118240

119241
```cpp
120-
242+
class Solution {
243+
private:
244+
vector<bool> sieve;
245+
void runSieve() {
246+
sieve.resize(100000, true);
247+
sieve[0] = false, sieve[1] = false;
248+
for (int i = 2; i < 1e5; ++i) {
249+
if (sieve[i]) {
250+
for (int j = 2 * i; j < 1e5; j += i) {
251+
sieve[j] = false;
252+
}
253+
}
254+
}
255+
}
256+
int solve(int n, int m) {
257+
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
258+
unordered_set<int> vis;
259+
pq.push({n, n});
260+
while (!pq.empty()) {
261+
int sum = pq.top().first, cur = pq.top().second;
262+
pq.pop();
263+
if (vis.find(cur) != vis.end()) continue;
264+
vis.insert(cur);
265+
if (cur == m) return sum;
266+
string s = to_string(cur);
267+
for (int i = 0; i < s.size(); ++i) {
268+
char c = s[i];
269+
if (s[i] < '9') {
270+
s[i]++;
271+
int next = stoi(s);
272+
if (!sieve[next] && vis.find(next) == vis.end()) {
273+
pq.push({sum + next, next});
274+
}
275+
s[i] = c;
276+
}
277+
if (s[i] > '0' && !(i == 0 && s[i] == '1')) {
278+
s[i]--;
279+
int next = stoi(s);
280+
if (!sieve[next] && vis.find(next) == vis.end()) {
281+
pq.push({sum + next, next});
282+
}
283+
s[i] = c;
284+
}
285+
}
286+
}
287+
return -1;
288+
}
289+
public:
290+
int minOperations(int n, int m) {
291+
runSieve();
292+
if (sieve[n] || sieve[m]) return -1;
293+
return solve(n, m);
294+
}
295+
};
121296
```
122297
123298
#### Go
124299
125300
```go
126-
301+
package main
302+
303+
import (
304+
"container/heap"
305+
"strconv"
306+
)
307+
308+
type MinHeap [][]int
309+
310+
func (h MinHeap) Len() int { return len(h) }
311+
func (h MinHeap) Less(i, j int) bool { return h[i][0] < h[j][0] }
312+
func (h MinHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
313+
func (h *MinHeap) Push(x interface{}) {
314+
*h = append(*h, x.([]int))
315+
}
316+
func (h *MinHeap) Pop() interface{} {
317+
old := *h
318+
n := len(old)
319+
x := old[n-1]
320+
*h = old[0 : n-1]
321+
return x
322+
}
323+
324+
var sieve []bool
325+
326+
func runSieve() {
327+
sieve = make([]bool, 100000)
328+
for i := range sieve {
329+
sieve[i] = true
330+
}
331+
sieve[0], sieve[1] = false, false
332+
for i := 2; i < 100000; i++ {
333+
if sieve[i] {
334+
for j := 2 * i; j < 100000; j += i {
335+
sieve[j] = false
336+
}
337+
}
338+
}
339+
}
340+
341+
func solve(n int, m int) int {
342+
pq := &MinHeap{}
343+
heap.Init(pq)
344+
heap.Push(pq, []int{n, n})
345+
visited := make(map[int]bool)
346+
347+
for pq.Len() > 0 {
348+
top := heap.Pop(pq).([]int)
349+
sum, cur := top[0], top[1]
350+
351+
if visited[cur] {
352+
continue
353+
}
354+
visited[cur] = true
355+
356+
if cur == m {
357+
return sum
358+
}
359+
360+
s := []rune(strconv.Itoa(cur))
361+
for i := 0; i < len(s); i++ {
362+
c := s[i]
363+
364+
if s[i] < '9' {
365+
s[i]++
366+
next, _ := strconv.Atoi(string(s))
367+
if !sieve[next] && !visited[next] {
368+
heap.Push(pq, []int{sum + next, next})
369+
}
370+
s[i] = c
371+
}
372+
373+
if s[i] > '0' && !(i == 0 && s[i] == '1') {
374+
s[i]--
375+
next, _ := strconv.Atoi(string(s))
376+
if !sieve[next] && !visited[next] {
377+
heap.Push(pq, []int{sum + next, next})
378+
}
379+
s[i] = c
380+
}
381+
}
382+
}
383+
384+
return -1
385+
}
386+
387+
func minOperations(n int, m int) int {
388+
runSieve()
389+
if sieve[n] || sieve[m] {
390+
return -1
391+
}
392+
return solve(n, m)
393+
}
127394
```
128395

129396
<!-- tabs:end -->

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /