Skip to content

Commit b576738

Browse files
add 2566
1 parent 0d0fdca commit b576738

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|------|----------------|------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|----------------------------------|-------------
11+
| 2566 |[Maximum Difference by Remapping a Digit](https://leetcode.com/problems/maximum-difference-by-remapping-a-digit/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2566.java) | | Easy |
1112
| 2562 |[Find the Array Concatenation Value](https://leetcode.com/problems/find-the-array-concatenation-value/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2562.java) | | Easy |
1213
| 2559 |[Count Vowel Strings in Ranges](https://leetcode.com/problems/count-vowel-strings-in-ranges/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2559.java) | | Medium |
1314
| 2558 |[Take Gifts From the Richest Pile](https://leetcode.com/problems/take-gifts-from-the-richest-pile/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2558.java) | | Easy |
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class _2566 {
7+
public static class Solution1 {
8+
public int minMaxDifference(int num) {
9+
List<Integer> digits = new ArrayList<>();
10+
while (num != 0) {
11+
digits.add(num % 10);
12+
num /= 10;
13+
}
14+
int toReplace = Integer.MAX_VALUE;
15+
List<Integer> maxDigits = new ArrayList<>();
16+
for (int i = digits.size() - 1; i >= 0; i--) {
17+
if (toReplace == Integer.MAX_VALUE && digits.get(i) != 9) {
18+
toReplace = digits.get(i);
19+
maxDigits.add(9);
20+
} else if (digits.get(i) == toReplace) {
21+
maxDigits.add(9);
22+
} else {
23+
maxDigits.add(digits.get(i));
24+
}
25+
}
26+
int max = 0;
27+
int times = 1;
28+
for (int i = maxDigits.size() - 1; i >= 0; i--) {
29+
max += maxDigits.get(i) * times;
30+
times *= 10;
31+
}
32+
33+
toReplace = Integer.MIN_VALUE;
34+
List<Integer> minDigits = new ArrayList<>();
35+
for (int i = digits.size() - 1; i >= 0; i--) {
36+
if (toReplace == Integer.MIN_VALUE && digits.get(i) != 0) {
37+
toReplace = digits.get(i);
38+
minDigits.add(0);
39+
} else if (digits.get(i) == toReplace) {
40+
minDigits.add(0);
41+
} else {
42+
minDigits.add(digits.get(i));
43+
}
44+
}
45+
int min = 0;
46+
times = 1;
47+
for (int i = minDigits.size() - 1; i >= 0; i--) {
48+
min += minDigits.get(i) * times;
49+
times *= 10;
50+
}
51+
return max - min;
52+
}
53+
}
54+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._2566;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _2566Test {
10+
private static _2566.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _2566.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(99009, solution1.minMaxDifference(11891));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(900, solution1.minMaxDifference(456));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(99, solution1.minMaxDifference(90));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)