Skip to content

Commit 4f69fc3

Browse files
committed
Add solution #2342
1 parent 9e962f6 commit 4f69fc3

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@
527527
2244|[Minimum Rounds to Complete All Tasks](./2244-minimum-rounds-to-complete-all-tasks.js)|Medium|
528528
2300|[Successful Pairs of Spells and Potions](./2300-successful-pairs-of-spells-and-potions.js)|Medium|
529529
2336|[Smallest Number in Infinite Set](./2336-smallest-number-in-infinite-set.js)|Medium|
530+
2342|[Max Sum of a Pair With Equal Sum of Digits](./2342-max-sum-of-a-pair-with-equal-sum-of-digits.js)|Medium|
530531
2349|[Design a Number Container System](./2349-design-a-number-container-system.js)|Medium|
531532
2352|[Equal Row and Column Pairs](./2352-equal-row-and-column-pairs.js)|Medium|
532533
2364|[Count Number of Bad Pairs](./2364-count-number-of-bad-pairs.js)|Medium|
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* 2342. Max Sum of a Pair With Equal Sum of Digits
3+
* https://leetcode.com/problems/max-sum-of-a-pair-with-equal-sum-of-digits/
4+
* Difficulty: Medium
5+
*
6+
* You are given a 0-indexed array nums consisting of positive integers. You can choose two
7+
* indices i and j, such that i != j, and the sum of digits of the number nums[i] is equal
8+
* to that of nums[j].
9+
*
10+
* Return the maximum value of nums[i] + nums[j] that you can obtain over all possible indices
11+
* i and j that satisfy the conditions.
12+
*/
13+
14+
/**
15+
* @param {number[]} nums
16+
* @return {number}
17+
*/
18+
var maximumSum = function(nums) {
19+
const map = new Map();
20+
21+
return nums.reduce((result, n) => {
22+
const key = n.toString().split('').reduce((sum, n) => +n + sum, 0);
23+
result = !map.has(key) ? result : Math.max(result, n + map.get(key));
24+
map.set(key, Math.max(map.get(key) ?? 0, n));
25+
26+
return result;
27+
}, -1);
28+
};

0 commit comments

Comments
 (0)