Skip to content

Commit 9e1aceb

Browse files
add 3982
1 parent 6c1d424 commit 9e1aceb

3 files changed

Lines changed: 50 additions & 0 deletions

File tree

  • paginated_contents/algorithms/4th_thousand
  • src

paginated_contents/algorithms/4th_thousand/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| # | Title | Solutions | Video | Difficulty | Tag
22
|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|------------|----------------------------------------------------------------------
3+
| 3982 | [Sum of Integers with Maximum Digit Range](https://leetcode.com/problems/sum-of-integers-with-maximum-digit-range/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3982.java) | | Easy |
34
| 3908 | [Valid Digit Number](https://leetcode.com/problems/valid-digit-number/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3908.java) | | Easy |
45
| 3502 | [Minimum Cost to Reach Every Position](https://leetcode.com/problems/minimum-cost-to-reach-every-position/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3502.java) | | Easy |
56
| 3491 | [Phone Number Prefix](https://leetcode.com/problems/phone-number-prefix/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3491.java) | | Easy |
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fishercoder.solutions.fourththousand;
2+
3+
import java.util.TreeMap;
4+
5+
public class _3982 {
6+
public static class Solution1 {
7+
public int maxDigitRange(int[] nums) {
8+
TreeMap<Integer, Integer> map = new TreeMap<>();
9+
for (int num : nums) {
10+
int digitRange = computeDigitRange(num);
11+
map.put(digitRange, map.getOrDefault(digitRange, 0) + num);
12+
}
13+
return map.lastEntry().getValue();
14+
}
15+
16+
private int computeDigitRange(int num) {
17+
int smallest = Integer.MAX_VALUE;
18+
int biggest = Integer.MIN_VALUE;
19+
while (num > 0) {
20+
int digit = num % 10;
21+
smallest = Math.min(smallest, digit);
22+
biggest = Math.max(biggest, digit);
23+
num /= 10;
24+
}
25+
return biggest - smallest;
26+
}
27+
}
28+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.fishercoder.fourththousand;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import com.fishercoder.solutions.fourththousand._3982;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class _3982Test {
10+
private _3982.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _3982.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(6074, solution1.maxDigitRange(new int[] {5724, 111, 350}));
20+
}
21+
}

0 commit comments

Comments
 (0)