From 16362f39954a8cc67a7de962a5d682491e6a0f32 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Wed, 12 Feb 2025 09:06:27 +0800 Subject: [PATCH] Add solution and test-cases for problem 2342 --- .../README.md | 28 +++++++------- .../Solution.go | 38 ++++++++++++++++++- .../Solution_test.go | 13 +++---- 3 files changed, 56 insertions(+), 23 deletions(-) diff --git a/leetcode/2301-2400/2342.Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits/README.md b/leetcode/2301-2400/2342.Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits/README.md index 5f594706e..47421e699 100755 --- a/leetcode/2301-2400/2342.Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits/README.md +++ b/leetcode/2301-2400/2342.Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits/README.md @@ -1,28 +1,28 @@ # [2342.Max Sum of a Pair With Equal Sum of Digits][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## 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: a = "11", b = "1" -Output: "100" +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:** -### 思路1 -> ... -Max Sum of a Pair With Equal Sum of Digits -```go ``` - +Input: nums = [10,12,19,14] +Output: -1 +Explanation: There are no two numbers that satisfy the conditions, so we return -1. +``` ## 结语 diff --git a/leetcode/2301-2400/2342.Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits/Solution.go b/leetcode/2301-2400/2342.Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits/Solution.go index d115ccf5e..5dacb201a 100644 --- a/leetcode/2301-2400/2342.Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits/Solution.go +++ b/leetcode/2301-2400/2342.Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits/Solution.go @@ -1,5 +1,39 @@ package Solution -func Solution(x bool) bool { - return x +func countOfOne2342(n int) int { + one := 0 + for n > 0 { + one += n % 10 + n /= 10 + } + return one +} + +func Solution(nums []int) int { + ans := -1 + sum := make(map[int][2]int) + for i := range nums { + one := countOfOne2342(nums[i]) + v, ok := sum[one] + if !ok { + sum[one] = [2]int{nums[i], -1} + continue + } + if nums[i] >= sum[one][0] { + v[1] = v[0] + v[0] = nums[i] + sum[one] = v + continue + } + if nums[i] > sum[one][1] { + v[1] = nums[i] + sum[one] = v + } + } + for _, v := range sum { + if v[0] != -1 && v[1] != -1 { + ans = max(ans, v[0]+v[1]) + } + } + return ans } diff --git a/leetcode/2301-2400/2342.Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits/Solution_test.go b/leetcode/2301-2400/2342.Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits/Solution_test.go index 14ff50eb4..34ac37b8a 100644 --- a/leetcode/2301-2400/2342.Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits/Solution_test.go +++ b/leetcode/2301-2400/2342.Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{18, 43, 36, 13, 7}, 54}, + {"TestCase2", []int{10, 12, 19, 14}, -1}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }