Skip to content

Commit fc5b174

Browse files
committed
feat(leetcode): add No.2815
1 parent 02ab09e commit fc5b174

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

2815.Max-Pair-Sum-in-an-Array.java

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// https://leetcode.com/problems/max-pair-sum-in-an-array/description/
2+
// algorithms
3+
// Easy (57.85%)
4+
// Total Accepted: 30.2K
5+
// Total Submissions: 52.2K
6+
7+
8+
class Solution {
9+
10+
public int maxSum(int[] nums) {
11+
Map<Integer, List<Integer>> map = new HashMap<>();
12+
13+
for (int n : nums) {
14+
int maxDigit = getMaxDigit(n);
15+
16+
List<Integer> list = map.get(maxDigit);
17+
if (list == null) {
18+
list = new ArrayList<>();
19+
map.put(maxDigit, list);
20+
}
21+
22+
list.add(n);
23+
}
24+
25+
int res = -1;
26+
for (Map.Entry<Integer, List<Integer>> entry : map.entrySet()) {
27+
List<Integer> list = entry.getValue();
28+
int size = list.size();
29+
if (size > 1) {
30+
Collections.sort(list);
31+
res = Math.max(res, list.get(size - 1) + list.get(size - 2));
32+
}
33+
}
34+
35+
return res;
36+
}
37+
38+
private int getMaxDigit(int n) {
39+
int maxDigit = -1;
40+
while (n > 0) {
41+
maxDigit = Math.max(maxDigit, n % 10);
42+
n = n / 10;
43+
}
44+
45+
return maxDigit;
46+
}
47+
48+
}

0 commit comments

Comments
 (0)