2342. Max Sum of a Pair With Equal Sum of Digits
重點在於: the sum of digits of the number nums[i] is equal to that of nums[j]. 這句英文,
指的是 例如[18,43,36,13,7], 其中18 sum of digits 是 1+8 =9, 36 是 3+ 6 =9 兩者都是 9,
不是兩數的最大公因數 [18, 36] both numbers have a sum of digits equal to 9, and their sum is 18+36 =54
[18,43,36,13,7] 轉化之後為 [9,7,9,4,7]。所以,(0, 2), (1, 4)相同
其他的條件式:
nums 從0開始, 0-indexed
2.全部都是正數integers, consisting of positive integers.
3.回傳最大值maximum, 比較後的最大值, Return the maximum value of nums[i] + nums[j] that you can obtain over all possible indices I and j that satisfy the conditions.
class Solution {
public:
int maximumSum(vector& nums) {
int res = -1, d_n[82] = {}; // 9 * 9
/*[18,43,36,13,7]*/
for (int n : nums) {
int d = 0;
for (int nn = n; nn; nn /= 10)
d += nn % 10;
if (d_n[d])
res = max(res, d_n[d] + n);
d_n[d] = max(d_n[d], n);
}
return res;
}
};
以上。
祝你順心。
Billour Ou
歐育溙
2022/7/23
參考
Leetcode
HackerRank