Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9b3fc6b

Browse files
solves #2815: Max Pair Sum in an Array in java
1 parent bbcc01c commit 9b3fc6b

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

‎README.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,4 +843,7 @@
843843
| 2788 | [Split Strings by Separator](https://leetcode.com/problems/split-strings-by-separator) | [![Java](assets/java.png)](src/SplitStringsBySeparator.java) | |
844844
| 2798 | [Number of Employees Who Met the Target](https://leetcode.com/problems/number-of-employees-who-met-the-target) | [![Java](assets/java.png)](src/NumberOfEmployeesWhoMetTheTarget.java) | |
845845
| 2806 | [Account Balance After Rounded Purchase](https://leetcode.com/problems/account-balance-after-rounded-purchase) | [![Java](assets/java.png)](src/AccountBalanceAfterRoundedPurchase.java) | |
846-
| 2810 | [Faulty Keyboard](https://leetcode.com/problems/faulty-keyboard) | [![Java](assets/java.png)](src/FaultyKeyboard.java) | |
846+
| 2810 | [Faulty Keyboard](https://leetcode.com/problems/faulty-keyboard) | [![Java](assets/java.png)](src/FaultyKeyboard.java) | |
847+
| 2815 | [Max Pair Sum in an Array](https://leetcode.com/problems/max-pair-sum-in-an-array) | [![Java](assets/java.png)](src/MaxPairSumInAnArray.java) | |
848+
| 2824 | [Count Pairs Whose Sum is Less than Target](https://leetcode.com/problems/count-pairs-whose-sum-is-less-than-target) | [![Java](assets/java.png)](src/) | |
849+
| 2828 | [Check if a String Is an Acronym of Words](https://leetcode.com/problems/check-if-a-string-is-an-acronym-of-words) | [![Java](assets/java.png)](src/) | |

‎src/MaxPairSumInAnArray.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// https://leetcode.com/problems/max-pair-sum-in-an-array
2+
// T: O(N)
3+
// S: O(1)
4+
5+
public class MaxPairSumInAnArray {
6+
public int maxSum(int[] nums) {
7+
final int[] digitMaxSum = new int[9];
8+
int maxPairSum = 0;
9+
for (int element : nums) {
10+
final int maxDigit = getMaxDigit(element);
11+
if (digitMaxSum[maxDigit - 1] != 0) {
12+
maxPairSum = Math.max(maxPairSum, digitMaxSum[maxDigit - 1] + element);
13+
}
14+
digitMaxSum[maxDigit - 1] = Math.max(digitMaxSum[maxDigit - 1], element);
15+
}
16+
return maxPairSum == 0 ? -1 : maxPairSum;
17+
}
18+
19+
private int getMaxDigit(int number) {
20+
final String string = number + "";
21+
int maxDigit = 0;
22+
for (int i = 0 ; i < string.length() ; i++) {
23+
maxDigit = Math.max(maxDigit, string.charAt(i) - '0');
24+
}
25+
return maxDigit;
26+
}
27+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /