Skip to content

Commit

Permalink
2815
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangshanmeta committed Jan 17, 2025
1 parent 09a0a5f commit 4fbb4a2
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/2815.max-pair-sum-in-an-array.2902/solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution {
public int maxSum(int[] nums) {
int[] maxs = new int[10];
int[] nexts = new int[10];
for(int num : nums){
int index = calc(num);
if(maxs[index]<=num){
nexts[index] = maxs[index];
maxs[index] = num;
}else if(nexts[index]<num){
nexts[index] = num;
}
}
int result = -1;
for(int i=0;i<10;i++){
if(nexts[i] != 0){
result = Math.max(result,maxs[i]+nexts[i]);
}
}
return result;
}

private int calc(int num){
int result = 0;
while (num != 0){
result = Math.max(result,num%10);
num /= 10;
}
return result;
}
}

0 comments on commit 4fbb4a2

Please sign in to comment.