1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| class Solution { public int maximumSum(int[] nums) { int maxSum = -1;
Map<Integer, Integer> digitSumToMaxNumMap = new HashMap<>(); for (int num : nums) { int digitSum = getDigitSum(num); Integer anotherNum = digitSumToMaxNumMap.get(digitSum); if (anotherNum != null) { maxSum = Math.max(maxSum, num + anotherNum); } digitSumToMaxNumMap.put(digitSum, Math.max(num, anotherNum != null ? anotherNum : num)); }
return maxSum; }
private int getDigitSum(int num) { int digitSum = 0; while (num != 0) { digitSum += num % 10; num /= 10; } return digitSum; } }
|
References
2342. Max Sum of a Pair With Equal Sum of Digits