3377. Digit Operations to Make Two Integers Equal Medium
1/**
2 * [3377] Digit Operations to Make Two Integers Equal
3 *
4 * You are given two integers n and m that consist of the same number of digits.
5 * You can perform the following operations any number of times:
6 *
7 * Choose any digit from n that is not 9 and increase it by 1.
8 * Choose any digit from n that is not 0 and decrease it by 1.
9 *
10 * The integer n must not be a <span data-keyword="prime-number">prime</span> number at any point, including its original value and after each operation.
11 * The cost of a transformation is the sum of all values that n takes throughout the operations performed.
12 * Return the minimum cost to transform n into m. If it is impossible, return -1.
13 *
14 * <strong class="example">Example 1:
15 * <div class="example-block">
16 * Input: <span class="example-io">n = 10, m = 12</span>
17 * Output: <span class="example-io">85</span>
18 * Explanation:
19 * We perform the following operations:
20 *
21 * Increase the first digit, now n = <u>2</u>0.
22 * Increase the second digit, now n = 2<u>1</u>.
23 * Increase the second digit, now n = 2<u>2</u>.
24 * Decrease the first digit, now n = <u>1</u>2.
25 * </div>
26 * <strong class="example">Example 2:
27 * <div class="example-block">
28 * Input: <span class="example-io">n = 4, m = 8</span>
29 * Output: <span class="example-io">-1</span>
30 * Explanation:
31 * It is impossible to make n equal to m.
32 * </div>
33 * <strong class="example">Example 3:
34 * <div class="example-block">
35 * Input: <span class="example-io">n = 6, m = 2</span>
36 * Output: <span class="example-io">-1</span>
37 * Explanation:
38 * Since 2 is already a prime, we can't make n equal to m.
39 * </div>
40 *
41 * Constraints:
42 *
43 * 1 <= n, m < 10^4
44 * n and m consist of the same number of digits.
45 *
46 */
47pub struct Solution {}
48
49// problem: https://leetcode.com/problems/digit-operations-to-make-two-integers-equal/
50// discuss: https://leetcode.com/problems/digit-operations-to-make-two-integers-equal/discuss/?currentPage=1&orderBy=most_votes&query=
51
52// submission codes start here
53
54impl Solution {
55 pub fn min_operations(n: i32, m: 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_3377() {
68 }
69}
70
Back
© 2025 bowen.ge All Rights Reserved.