diff --git a/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/README.md b/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/README.md index cf1229c32e0de..9ab35abd4ccbd 100644 --- a/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/README.md +++ b/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/README.md @@ -105,25 +105,292 @@ tags: #### Python3 ```python - +import heapq + +class Solution: + def __init__(self): + self.sieve = [] + + def run_sieve(self): + self.sieve = [True] * 100000 + self.sieve[0], self.sieve[1] = False, False + for i in range(2, 100000): + if self.sieve[i]: + for j in range(2 * i, 100000, i): + self.sieve[j] = False + + def solve(self, n, m): + pq = [] + heapq.heappush(pq, (n, n)) + visited = set() + + while pq: + sum_, cur = heapq.heappop(pq) + + if cur in visited: + continue + visited.add(cur) + + if cur == m: + return sum_ + + s = list(str(cur)) + for i in range(len(s)): + c = s[i] + + if s[i] < '9': + s[i] = chr(ord(s[i]) + 1) + next_ = int(''.join(s)) + if not self.sieve[next_] and next_ not in visited: + heapq.heappush(pq, (sum_ + next_, next_)) + s[i] = c + + if s[i] > '0' and not (i == 0 and s[i] == '1'): + s[i] = chr(ord(s[i]) - 1) + next_ = int(''.join(s)) + if not self.sieve[next_] and next_ not in visited: + heapq.heappush(pq, (sum_ + next_, next_)) + s[i] = c + + return -1 + + def minOperations(self, n, m): + self.run_sieve() + if self.sieve[n] or self.sieve[m]: + return -1 + return self.solve(n, m) ``` #### Java ```java - +class Solution { + private boolean[] sieve; + + private void runSieve() { + sieve = new boolean[100000]; + Arrays.fill(sieve, true); + sieve[0] = false; + sieve[1] = false; + for (int i = 2; i < 100000; i++) { + if (sieve[i]) { + for (int j = 2 * i; j < 100000; j += i) { + sieve[j] = false; + } + } + } + } + + private int solve(int n, int m) { + PriorityQueue pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[0])); + pq.add(new int[]{n, n}); + Set visited = new HashSet<>(); + + while (!pq.isEmpty()) { + int[] top = pq.poll(); + int sum = top[0], cur = top[1]; + + if (visited.contains(cur)) { + continue; + } + visited.add(cur); + + if (cur == m) { + return sum; + } + + char[] s = String.valueOf(cur).toCharArray(); + for (int i = 0; i < s.length; i++) { + char c = s[i]; + + if (s[i] < '9') { + s[i] = (char) (s[i] + 1); + int next = Integer.parseInt(new String(s)); + if (!sieve[next] && !visited.contains(next)) { + pq.add(new int[]{sum + next, next}); + } + s[i] = c; + } + + if (s[i] > '0' && !(i == 0 && s[i] == '1')) { + s[i] = (char) (s[i] - 1); + int next = Integer.parseInt(new String(s)); + if (!sieve[next] && !visited.contains(next)) { + pq.add(new int[]{sum + next, next}); + } + s[i] = c; + } + } + } + + return -1; + } + + public int minOperations(int n, int m) { + runSieve(); + if (sieve[n] || sieve[m]) { + return -1; + } + return solve(n, m); + } +} ``` #### C++ ```cpp - +class Solution { +private: + vector sieve; + void runSieve() { + sieve.resize(100000, true); + sieve[0] = false, sieve[1] = false; + for (int i = 2; i < 1e5; ++i) { + if (sieve[i]) { + for (int j = 2 * i; j < 1e5; j += i) { + sieve[j] = false; + } + } + } + } + int solve(int n, int m) { + priority_queue, vector>, greater>> pq; + unordered_set vis; + pq.push({n, n}); + while (!pq.empty()) { + int sum = pq.top().first, cur = pq.top().second; + pq.pop(); + if (vis.find(cur) != vis.end()) continue; + vis.insert(cur); + if (cur == m) return sum; + string s = to_string(cur); + for (int i = 0; i < s.size(); ++i) { + char c = s[i]; + if (s[i] < '9') { + s[i]++; + int next = stoi(s); + if (!sieve[next] && vis.find(next) == vis.end()) { + pq.push({sum + next, next}); + } + s[i] = c; + } + if (s[i] > '0' && !(i == 0 && s[i] == '1')) { + s[i]--; + int next = stoi(s); + if (!sieve[next] && vis.find(next) == vis.end()) { + pq.push({sum + next, next}); + } + s[i] = c; + } + } + } + return -1; + } +public: + int minOperations(int n, int m) { + runSieve(); + if (sieve[n] || sieve[m]) return -1; + return solve(n, m); + } +}; ``` #### Go ```go - +package main + +import ( + "container/heap" + "strconv" +) + +type MinHeap [][]int + +func (h MinHeap) Len() int { return len(h) } +func (h MinHeap) Less(i, j int) bool { return h[i][0] < h[j][0] } +func (h MinHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *MinHeap) Push(x interface{}) { + *h = append(*h, x.([]int)) +} +func (h *MinHeap) Pop() interface{} { + old := *h + n := len(old) + x := old[n-1] + *h = old[0 : n-1] + return x +} + +var sieve []bool + +func runSieve() { + sieve = make([]bool, 100000) + for i := range sieve { + sieve[i] = true + } + sieve[0], sieve[1] = false, false + for i := 2; i < 100000; i++ { + if sieve[i] { + for j := 2 * i; j < 100000; j += i { + sieve[j] = false + } + } + } +} + +func solve(n int, m int) int { + pq := &MinHeap{} + heap.Init(pq) + heap.Push(pq, []int{n, n}) + visited := make(map[int]bool) + + for pq.Len() > 0 { + top := heap.Pop(pq).([]int) + sum, cur := top[0], top[1] + + if visited[cur] { + continue + } + visited[cur] = true + + if cur == m { + return sum + } + + s := []rune(strconv.Itoa(cur)) + for i := 0; i < len(s); i++ { + c := s[i] + + if s[i] < '9' { + s[i]++ + next, _ := strconv.Atoi(string(s)) + if !sieve[next] && !visited[next] { + heap.Push(pq, []int{sum + next, next}) + } + s[i] = c + } + + if s[i] > '0' && !(i == 0 && s[i] == '1') { + s[i]-- + next, _ := strconv.Atoi(string(s)) + if !sieve[next] && !visited[next] { + heap.Push(pq, []int{sum + next, next}) + } + s[i] = c + } + } + } + + return -1 +} + +func minOperations(n int, m int) int { + runSieve() + if sieve[n] || sieve[m] { + return -1 + } + return solve(n, m) +} ``` diff --git a/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/README_EN.md b/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/README_EN.md index a98d52aa6d23c..35cba5de2bebd 100644 --- a/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/README_EN.md +++ b/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/README_EN.md @@ -102,25 +102,292 @@ tags: #### Python3 ```python - +import heapq + +class Solution: + def __init__(self): + self.sieve = [] + + def run_sieve(self): + self.sieve = [True] * 100000 + self.sieve[0], self.sieve[1] = False, False + for i in range(2, 100000): + if self.sieve[i]: + for j in range(2 * i, 100000, i): + self.sieve[j] = False + + def solve(self, n, m): + pq = [] + heapq.heappush(pq, (n, n)) + visited = set() + + while pq: + sum_, cur = heapq.heappop(pq) + + if cur in visited: + continue + visited.add(cur) + + if cur == m: + return sum_ + + s = list(str(cur)) + for i in range(len(s)): + c = s[i] + + if s[i] < '9': + s[i] = chr(ord(s[i]) + 1) + next_ = int(''.join(s)) + if not self.sieve[next_] and next_ not in visited: + heapq.heappush(pq, (sum_ + next_, next_)) + s[i] = c + + if s[i] > '0' and not (i == 0 and s[i] == '1'): + s[i] = chr(ord(s[i]) - 1) + next_ = int(''.join(s)) + if not self.sieve[next_] and next_ not in visited: + heapq.heappush(pq, (sum_ + next_, next_)) + s[i] = c + + return -1 + + def minOperations(self, n, m): + self.run_sieve() + if self.sieve[n] or self.sieve[m]: + return -1 + return self.solve(n, m) ``` #### Java ```java - +class Solution { + private boolean[] sieve; + + private void runSieve() { + sieve = new boolean[100000]; + Arrays.fill(sieve, true); + sieve[0] = false; + sieve[1] = false; + for (int i = 2; i < 100000; i++) { + if (sieve[i]) { + for (int j = 2 * i; j < 100000; j += i) { + sieve[j] = false; + } + } + } + } + + private int solve(int n, int m) { + PriorityQueue pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[0])); + pq.add(new int[]{n, n}); + Set visited = new HashSet<>(); + + while (!pq.isEmpty()) { + int[] top = pq.poll(); + int sum = top[0], cur = top[1]; + + if (visited.contains(cur)) { + continue; + } + visited.add(cur); + + if (cur == m) { + return sum; + } + + char[] s = String.valueOf(cur).toCharArray(); + for (int i = 0; i < s.length; i++) { + char c = s[i]; + + if (s[i] < '9') { + s[i] = (char) (s[i] + 1); + int next = Integer.parseInt(new String(s)); + if (!sieve[next] && !visited.contains(next)) { + pq.add(new int[]{sum + next, next}); + } + s[i] = c; + } + + if (s[i] > '0' && !(i == 0 && s[i] == '1')) { + s[i] = (char) (s[i] - 1); + int next = Integer.parseInt(new String(s)); + if (!sieve[next] && !visited.contains(next)) { + pq.add(new int[]{sum + next, next}); + } + s[i] = c; + } + } + } + + return -1; + } + + public int minOperations(int n, int m) { + runSieve(); + if (sieve[n] || sieve[m]) { + return -1; + } + return solve(n, m); + } +} ``` #### C++ ```cpp - +class Solution { +private: + vector sieve; + void runSieve() { + sieve.resize(100000, true); + sieve[0] = false, sieve[1] = false; + for (int i = 2; i < 1e5; ++i) { + if (sieve[i]) { + for (int j = 2 * i; j < 1e5; j += i) { + sieve[j] = false; + } + } + } + } + int solve(int n, int m) { + priority_queue, vector>, greater>> pq; + unordered_set vis; + pq.push({n, n}); + while (!pq.empty()) { + int sum = pq.top().first, cur = pq.top().second; + pq.pop(); + if (vis.find(cur) != vis.end()) continue; + vis.insert(cur); + if (cur == m) return sum; + string s = to_string(cur); + for (int i = 0; i < s.size(); ++i) { + char c = s[i]; + if (s[i] < '9') { + s[i]++; + int next = stoi(s); + if (!sieve[next] && vis.find(next) == vis.end()) { + pq.push({sum + next, next}); + } + s[i] = c; + } + if (s[i] > '0' && !(i == 0 && s[i] == '1')) { + s[i]--; + int next = stoi(s); + if (!sieve[next] && vis.find(next) == vis.end()) { + pq.push({sum + next, next}); + } + s[i] = c; + } + } + } + return -1; + } +public: + int minOperations(int n, int m) { + runSieve(); + if (sieve[n] || sieve[m]) return -1; + return solve(n, m); + } +}; ``` #### Go ```go - +package main + +import ( + "container/heap" + "strconv" +) + +type MinHeap [][]int + +func (h MinHeap) Len() int { return len(h) } +func (h MinHeap) Less(i, j int) bool { return h[i][0] < h[j][0] } +func (h MinHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *MinHeap) Push(x interface{}) { + *h = append(*h, x.([]int)) +} +func (h *MinHeap) Pop() interface{} { + old := *h + n := len(old) + x := old[n-1] + *h = old[0 : n-1] + return x +} + +var sieve []bool + +func runSieve() { + sieve = make([]bool, 100000) + for i := range sieve { + sieve[i] = true + } + sieve[0], sieve[1] = false, false + for i := 2; i < 100000; i++ { + if sieve[i] { + for j := 2 * i; j < 100000; j += i { + sieve[j] = false + } + } + } +} + +func solve(n int, m int) int { + pq := &MinHeap{} + heap.Init(pq) + heap.Push(pq, []int{n, n}) + visited := make(map[int]bool) + + for pq.Len() > 0 { + top := heap.Pop(pq).([]int) + sum, cur := top[0], top[1] + + if visited[cur] { + continue + } + visited[cur] = true + + if cur == m { + return sum + } + + s := []rune(strconv.Itoa(cur)) + for i := 0; i < len(s); i++ { + c := s[i] + + if s[i] < '9' { + s[i]++ + next, _ := strconv.Atoi(string(s)) + if !sieve[next] && !visited[next] { + heap.Push(pq, []int{sum + next, next}) + } + s[i] = c + } + + if s[i] > '0' && !(i == 0 && s[i] == '1') { + s[i]-- + next, _ := strconv.Atoi(string(s)) + if !sieve[next] && !visited[next] { + heap.Push(pq, []int{sum + next, next}) + } + s[i] = c + } + } + } + + return -1 +} + +func minOperations(n int, m int) int { + runSieve() + if sieve[n] || sieve[m] { + return -1 + } + return solve(n, m) +} ``` diff --git a/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/Solution.cpp b/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/Solution.cpp new file mode 100644 index 0000000000000..ea2df83e39a04 --- /dev/null +++ b/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/Solution.cpp @@ -0,0 +1,54 @@ +class Solution { +private: + vector sieve; + void runSieve() { + sieve.resize(100000, true); + sieve[0] = false, sieve[1] = false; + for (int i = 2; i < 1e5; ++i) { + if (sieve[i]) { + for (int j = 2 * i; j < 1e5; j += i) { + sieve[j] = false; + } + } + } + } + int solve(int n, int m) { + priority_queue, vector>, greater>> pq; + unordered_set vis; + pq.push({n, n}); + while (!pq.empty()) { + int sum = pq.top().first, cur = pq.top().second; + pq.pop(); + if (vis.find(cur) != vis.end()) continue; + vis.insert(cur); + if (cur == m) return sum; + string s = to_string(cur); + for (int i = 0; i < s.size(); ++i) { + char c = s[i]; + if (s[i] < '9') { + s[i]++; + int next = stoi(s); + if (!sieve[next] && vis.find(next) == vis.end()) { + pq.push({sum + next, next}); + } + s[i] = c; + } + if (s[i] > '0' && !(i == 0 && s[i] == '1')) { + s[i]--; + int next = stoi(s); + if (!sieve[next] && vis.find(next) == vis.end()) { + pq.push({sum + next, next}); + } + s[i] = c; + } + } + } + return -1; + } +public: + int minOperations(int n, int m) { + runSieve(); + if (sieve[n] || sieve[m]) return -1; + return solve(n, m); + } +}; diff --git a/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/Solution.go b/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/Solution.go new file mode 100644 index 0000000000000..755e367de4721 --- /dev/null +++ b/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/Solution.go @@ -0,0 +1,93 @@ +package main + +import ( + "container/heap" + "strconv" +) + +type MinHeap [][]int + +func (h MinHeap) Len() int { return len(h) } +func (h MinHeap) Less(i, j int) bool { return h[i][0] < h[j][0] } +func (h MinHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *MinHeap) Push(x interface{}) { + *h = append(*h, x.([]int)) +} +func (h *MinHeap) Pop() interface{} { + old := *h + n := len(old) + x := old[n-1] + *h = old[0 : n-1] + return x +} + +var sieve []bool + +func runSieve() { + sieve = make([]bool, 100000) + for i := range sieve { + sieve[i] = true + } + sieve[0], sieve[1] = false, false + for i := 2; i < 100000; i++ { + if sieve[i] { + for j := 2 * i; j < 100000; j += i { + sieve[j] = false + } + } + } +} + +func solve(n int, m int) int { + pq := &MinHeap{} + heap.Init(pq) + heap.Push(pq, []int{n, n}) + visited := make(map[int]bool) + + for pq.Len() > 0 { + top := heap.Pop(pq).([]int) + sum, cur := top[0], top[1] + + if visited[cur] { + continue + } + visited[cur] = true + + if cur == m { + return sum + } + + s := []rune(strconv.Itoa(cur)) + for i := 0; i < len(s); i++ { + c := s[i] + + if s[i] < '9' { + s[i]++ + next, _ := strconv.Atoi(string(s)) + if !sieve[next] && !visited[next] { + heap.Push(pq, []int{sum + next, next}) + } + s[i] = c + } + + if s[i] > '0' && !(i == 0 && s[i] == '1') { + s[i]-- + next, _ := strconv.Atoi(string(s)) + if !sieve[next] && !visited[next] { + heap.Push(pq, []int{sum + next, next}) + } + s[i] = c + } + } + } + + return -1 +} + +func minOperations(n int, m int) int { + runSieve() + if sieve[n] || sieve[m] { + return -1 + } + return solve(n, m) +} diff --git a/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/Solution.java b/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/Solution.java new file mode 100644 index 0000000000000..a1fe469abf69d --- /dev/null +++ b/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/Solution.java @@ -0,0 +1,70 @@ +class Solution { + private boolean[] sieve; + + private void runSieve() { + sieve = new boolean[100000]; + Arrays.fill(sieve, true); + sieve[0] = false; + sieve[1] = false; + for (int i = 2; i < 100000; i++) { + if (sieve[i]) { + for (int j = 2 * i; j < 100000; j += i) { + sieve[j] = false; + } + } + } + } + + private int solve(int n, int m) { + PriorityQueue pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[0])); + pq.add(new int[]{n, n}); + Set visited = new HashSet<>(); + + while (!pq.isEmpty()) { + int[] top = pq.poll(); + int sum = top[0], cur = top[1]; + + if (visited.contains(cur)) { + continue; + } + visited.add(cur); + + if (cur == m) { + return sum; + } + + char[] s = String.valueOf(cur).toCharArray(); + for (int i = 0; i < s.length; i++) { + char c = s[i]; + + if (s[i] < '9') { + s[i] = (char) (s[i] + 1); + int next = Integer.parseInt(new String(s)); + if (!sieve[next] && !visited.contains(next)) { + pq.add(new int[]{sum + next, next}); + } + s[i] = c; + } + + if (s[i] > '0' && !(i == 0 && s[i] == '1')) { + s[i] = (char) (s[i] - 1); + int next = Integer.parseInt(new String(s)); + if (!sieve[next] && !visited.contains(next)) { + pq.add(new int[]{sum + next, next}); + } + s[i] = c; + } + } + } + + return -1; + } + + public int minOperations(int n, int m) { + runSieve(); + if (sieve[n] || sieve[m]) { + return -1; + } + return solve(n, m); + } +} \ No newline at end of file diff --git a/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/Solution.py b/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/Solution.py new file mode 100644 index 0000000000000..e5c4a464a5035 --- /dev/null +++ b/solution/3300-3399/3377.Digit Operations to Make Two Integers Equal/Solution.py @@ -0,0 +1,55 @@ +import heapq + + +class Solution: + def __init__(self): + self.sieve = [] + + def run_sieve(self): + self.sieve = [True] * 100000 + self.sieve[0], self.sieve[1] = False, False + for i in range(2, 100000): + if self.sieve[i]: + for j in range(2 * i, 100000, i): + self.sieve[j] = False + + def solve(self, n, m): + pq = [] + heapq.heappush(pq, (n, n)) + visited = set() + + while pq: + sum_, cur = heapq.heappop(pq) + + if cur in visited: + continue + visited.add(cur) + + if cur == m: + return sum_ + + s = list(str(cur)) + for i in range(len(s)): + c = s[i] + + if s[i] < '9': + s[i] = chr(ord(s[i]) + 1) + next_ = int(''.join(s)) + if not self.sieve[next_] and next_ not in visited: + heapq.heappush(pq, (sum_ + next_, next_)) + s[i] = c + + if s[i] > '0' and not (i == 0 and s[i] == '1'): + s[i] = chr(ord(s[i]) - 1) + next_ = int(''.join(s)) + if not self.sieve[next_] and next_ not in visited: + heapq.heappush(pq, (sum_ + next_, next_)) + s[i] = c + + return -1 + + def minOperations(self, n, m): + self.run_sieve() + if self.sieve[n] or self.sieve[m]: + return -1 + return self.solve(n, m)