Skip to content

Latest commit

 

History

History
241 lines (208 loc) · 5.18 KB

File metadata and controls

241 lines (208 loc) · 5.18 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 v in nums:
            x, y = v, 0
            while x:
                y += x % 10
                x //= 10
            d[y].append(v)
        ans = -1
        for vs in d.values():
            if len(vs) > 1:
                vs.sort(reverse=True)
                ans = max(ans, vs[0] + vs[1])
        return ans
class Solution:
    def maximumSum(self, nums: List[int]) -> int:
        ans = -1
        d = defaultdict(int)
        for v in nums:
            x, y = v, 0
            while x:
                y += x % 10
                x //= 10
            if y in d:
                ans = max(ans, d[y] + v)
            d[y] = max(d[y], v)
        return ans

Java

class Solution {
    public int maximumSum(int[] nums) {
        List<Integer>[] d = new List[100];
        Arrays.setAll(d, k -> new ArrayList<>());
        for (int v : nums) {
            int y = 0;
            for (int x = v; x > 0; x /= 10) {
                y += x % 10;
            }
            d[y].add(v);
        }
        int ans = -1;
        for (var vs : d) {
            int m = vs.size();
            if (m > 1) {
                Collections.sort(vs);
                ans = Math.max(ans, vs.get(m - 1) + vs.get(m - 2));
            }
        }
        return ans;
    }
}
class Solution {
    public int maximumSum(int[] nums) {
        int ans = -1;
        int[] d = new int[100];
        for (int v : nums) {
            int y = 0;
            for (int x = v; x > 0; x /= 10) {
                y += x % 10;
            }
            if (d[y] > 0) {
                ans = Math.max(ans, d[y] + v);
            }
            d[y] = Math.max(d[y], v);
        }
        return ans;
    }
}

C++

class Solution {
public:
    int maximumSum(vector<int>& nums) {
        vector<vector<int>> d(100);
        for (int& v : nums) {
            int y = 0;
            for (int x = v; x > 0; x /= 10) {
                y += x % 10;
            }
            d[y].emplace_back(v);
        }
        int ans = -1;
        for (auto& vs : d) {
            if (vs.size() > 1) {
                sort(vs.rbegin(), vs.rend());
                ans = max(ans, vs[0] + vs[1]);
            }
        }
        return ans;
    }
};
class Solution {
public:
    int maximumSum(vector<int>& nums) {
        int ans = -1;
        int d[100]{};
        for (int& v : nums) {
            int y = 0;
            for (int x = v; x; x /= 10) {
                y += x % 10;
            }
            if (d[y]) {
                ans = max(ans, d[y] + v);
            }
            d[y] = max(d[y], v);
        }
        return ans;
    }
};

Go

func maximumSum(nums []int) int {
	d := [100][]int{}
	for _, v := range nums {
		y := 0
		for x := v; x > 0; x /= 10 {
			y += x % 10
		}
		d[y] = append(d[y], v)
	}
	ans := -1
	for _, vs := range d {
		m := len(vs)
		if m > 1 {
			sort.Ints(vs)
			ans = max(ans, vs[m-1]+vs[m-2])
		}
	}
	return ans
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func maximumSum(nums []int) int {
	ans := -1
	d := [100]int{}
	for _, v := range nums {
		y := 0
		for x := v; x > 0; x /= 10 {
			y += x % 10
		}
		if d[y] > 0 {
			ans = max(ans, d[y]+v)
		}
		d[y] = max(d[y], v)
	}
	return ans
}

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

TypeScript

...