Skip to content

Commit 16362f3

Browse files
committed
Add solution and test-cases for problem 2342
1 parent d569e0c commit 16362f3

File tree

3 files changed

+56
-23
lines changed

3 files changed

+56
-23
lines changed

leetcode/2301-2400/2342.Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits/README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
# [2342.Max Sum of a Pair With Equal Sum of Digits][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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]`.
5+
6+
Return the **maximum** value of `nums[i] + nums[j]` that you can obtain over all possible indices `i` and `j` that satisfy the conditions.
77

88
**Example 1:**
99

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

15-
## 题意
16-
> ...
17-
18-
## 题解
19+
**Example 2:**
1920

20-
### 思路1
21-
> ...
22-
Max Sum of a Pair With Equal Sum of Digits
23-
```go
2421
```
25-
22+
Input: nums = [10,12,19,14]
23+
Output: -1
24+
Explanation: There are no two numbers that satisfy the conditions, so we return -1.
25+
```
2626

2727
## 结语
2828

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,39 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func countOfOne2342(n int) int {
4+
one := 0
5+
for n > 0 {
6+
one += n % 10
7+
n /= 10
8+
}
9+
return one
10+
}
11+
12+
func Solution(nums []int) int {
13+
ans := -1
14+
sum := make(map[int][2]int)
15+
for i := range nums {
16+
one := countOfOne2342(nums[i])
17+
v, ok := sum[one]
18+
if !ok {
19+
sum[one] = [2]int{nums[i], -1}
20+
continue
21+
}
22+
if nums[i] >= sum[one][0] {
23+
v[1] = v[0]
24+
v[0] = nums[i]
25+
sum[one] = v
26+
continue
27+
}
28+
if nums[i] > sum[one][1] {
29+
v[1] = nums[i]
30+
sum[one] = v
31+
}
32+
}
33+
for _, v := range sum {
34+
if v[0] != -1 && v[1] != -1 {
35+
ans = max(ans, v[0]+v[1])
36+
}
37+
}
38+
return ans
539
}

leetcode/2301-2400/2342.Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{18, 43, 36, 13, 7}, 54},
17+
{"TestCase2", []int{10, 12, 19, 14}, -1},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)