Skip to content

Latest commit

 

History

History
164 lines (136 loc) · 3.87 KB

File metadata and controls

164 lines (136 loc) · 3.87 KB

中文文档

Description

You are given a 0-indexed array nums consisting of positive integers. You can choose two indices i and j, such that i != j, and the sum of digits of the number nums[i] is equal to that of nums[j].

Return the maximum value of nums[i] + nums[j] that you can obtain over all possible indices i and j that satisfy the conditions.

 

Example 1:

Input: nums = [18,43,36,13,7]
Output: 54
Explanation: The pairs (i, j) that satisfy the conditions are:
- (0, 2), both numbers have a sum of digits equal to 9, and their sum is 18 + 36 = 54.
- (1, 4), both numbers have a sum of digits equal to 7, and their sum is 43 + 7 = 50.
So the maximum sum that we can obtain is 54.

Example 2:

Input: nums = [10,12,19,14]
Output: -1
Explanation: There are no two numbers that satisfy the conditions, so we return -1.

 

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 109

Solutions

Python3

class Solution:
    def maximumSum(self, nums: List[int]) -> int:
        d = defaultdict(list)
        for i, v in enumerate(nums):
            t = 0
            while v:
                t += v % 10
                v //= 10
            d[t].append(nums[i])
        ans = -1
        for v in d.values():
            v.sort(reverse=True)
            if len(v) > 1:
                ans = max(ans, v[0] + v[1])
        return ans

Java

class Solution {
    public int maximumSum(int[] nums) {
        Map<Integer, List<Integer>> d = new HashMap<>();
        for (int i = 0; i < nums.length; ++i) {
            int v = nums[i];
            int t = 0;
            while (v != 0) {
                t += v % 10;
                v /= 10;
            }
            d.computeIfAbsent(t, k -> new ArrayList<>()).add(nums[i]);
        }
        int ans = -1;
        for (List<Integer> v : d.values()) {
            int n = v.size();
            if (n > 1) {
                Collections.sort(v);
                ans = Math.max(ans, v.get(n - 1) + v.get(n - 2));
            }
        }
        return ans;
    }
}

C++

class Solution {
public:
    int maximumSum(vector<int>& nums) {
        unordered_map<int, vector<int>> d;
        for (int i = 0; i < nums.size(); ++i) {
            int v = nums[i];
            int t = 0;
            while (v) {
                t += v % 10;
                v /= 10;
            }
            d[t].push_back(nums[i]);
        }
        int ans = -1;
        for (auto& [_, v] : d) {
            int n = v.size();
            if (n > 1) {
                sort(v.begin(), v.end());
                ans = max(ans, v[n - 1] + v[n - 2]);
            }
        }
        return ans;
    }
};

Go

func maximumSum(nums []int) int {
	d := map[int][]int{}
	for i, v := range nums {
		t := 0
		for v > 0 {
			t += v % 10
			v /= 10
		}
		d[t] = append(d[t], nums[i])
	}
	ans := -1
	for _, v := range d {
		n := len(v)
		if n > 1 {
			sort.Ints(v)
			ans = max(ans, v[n-1]+v[n-2])
		}
	}
	return ans
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

TypeScript

...