43
43
44
44
<!-- 这里可写通用的实现逻辑 -->
45
45
46
- ** 方法一:哈希表 + 排序 **
46
+ ** 方法一:哈希表**
47
47
48
- 对于数组中的每个元素,计算其数位和,将其存入哈希表 $d$ 中,哈希表的键为数位和,值为数组中所有数位和为该键的元素组成的数组 。
48
+ 我们可以用一个哈希表 $d$ 记录每个数位和对应的最大值,初始化一个答案变量 $ans = -1$ 。
49
49
50
- 遍历哈希表 $d$,对于每个键值对,如果该键对应的数组长度大于 $1$,则对该数组进行降序排序,取前两个元素相加,更新答案 。
50
+ 接下来,我们遍历数组 $nums$,对于每个数 $v$,我们计算它的数位和 $x$,如果 $x$ 在哈希表 $d$ 中存在,那么我们就更新答案 $ans = \max(ans, d [ x ] + v)$。然后更新哈希表 $d [ x ] = \max(d [ x ] , v)$ 。
51
51
52
- 最终返回答案 。
52
+ 最后返回答案 $ans$ 即可 。
53
53
54
- 时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 ` nums ` 的长度 。
54
+ 由于 $nums$ 中的元素最大为 $10^9$,因此数位和最大为 $9 \times 9 = 81$,我们可以直接定义一个长度为 $100$ 的数组 $d$ 来代替哈希表 。
55
55
56
- ** 方法二:哈希表(优化)**
57
-
58
- 我们创建一个哈希表 $d$,其中哈希表的键为数位和,值为已遍历过的元素中数位和为该键的最大元素。
59
-
60
- 我们直接对数组 ` nums ` 进行遍历,对于每个元素 $v$,计算其数位和 $y$,如果 $d[ y] $ 存在,则更新答案为 $max(ans, v + d[ y] )$。然后我们更新 $d[ y] =max(d[ y] , v)$。
61
-
62
- 最终返回答案。
63
-
64
- 时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为数组 ` nums ` 的长度,而 $C$ 为数组 ` nums ` 的最大数位和。本题中 $nums[ i] \leq 10^9$,因此我们固定取 $C=100$ 即可。
56
+ 时间复杂度 $O(n \times \log M)$,空间复杂度 $O(D)$,其中 $n$ 是数组 $nums$ 的长度,而 $M$ 和 $D$ 分别是数组 $nums$ 中的元素的最大值和数位和的最大值。本题中 $M \leq 10^9$,$D \leq 81$。
65
57
66
58
<!-- tabs:start -->
67
59
72
64
``` python
73
65
class Solution :
74
66
def maximumSum (self , nums : List[int ]) -> int :
75
- d = defaultdict(list )
76
- for v in nums:
77
- x, y = v, 0
78
- while x:
79
- y += x % 10
80
- x //= 10
81
- d[y].append(v)
82
- ans = - 1
83
- for vs in d.values():
84
- if len (vs) > 1 :
85
- vs.sort(reverse = True )
86
- ans = max (ans, vs[0 ] + vs[1 ])
87
- return ans
88
- ```
89
-
90
- ``` python
91
- class Solution :
92
- def maximumSum (self , nums : List[int ]) -> int :
93
- ans = - 1
94
67
d = defaultdict(int )
68
+ ans = - 1
95
69
for v in nums:
96
- x, y = v, 0
97
- while x :
98
- y += x % 10
99
- x //= 10
100
- if y in d:
101
- ans = max (ans, d[y ] + v)
102
- d[y ] = max (d[y ], v)
70
+ x, y = 0 , v
71
+ while y :
72
+ x += y % 10
73
+ y //= 10
74
+ if x in d:
75
+ ans = max (ans, d[x ] + v)
76
+ d[x ] = max (d[x ], v)
103
77
return ans
104
78
```
105
79
@@ -110,42 +84,17 @@ class Solution:
110
84
``` java
111
85
class Solution {
112
86
public int maximumSum (int [] nums ) {
113
- List<Integer > [] d = new List [100 ];
114
- Arrays . setAll(d, k - > new ArrayList<> ());
115
- for (int v : nums) {
116
- int y = 0 ;
117
- for (int x = v; x > 0 ; x /= 10 ) {
118
- y += x % 10 ;
119
- }
120
- d[y]. add(v);
121
- }
122
- int ans = - 1 ;
123
- for (var vs : d) {
124
- int m = vs. size();
125
- if (m > 1 ) {
126
- Collections . sort(vs);
127
- ans = Math . max(ans, vs. get(m - 1 ) + vs. get(m - 2 ));
128
- }
129
- }
130
- return ans;
131
- }
132
- }
133
- ```
134
-
135
- ``` java
136
- class Solution {
137
- public int maximumSum (int [] nums ) {
138
- int ans = - 1 ;
139
87
int [] d = new int [100 ];
88
+ int ans = - 1 ;
140
89
for (int v : nums) {
141
- int y = 0 ;
142
- for (int x = v; x > 0 ; x /= 10 ) {
143
- y += x % 10 ;
90
+ int x = 0 ;
91
+ for (int y = v; y > 0 ; y /= 10 ) {
92
+ x += y % 10 ;
144
93
}
145
- if (d[y ] > 0 ) {
146
- ans = Math . max(ans, d[y ] + v);
94
+ if (d[x ] > 0 ) {
95
+ ans = Math . max(ans, d[x ] + v);
147
96
}
148
- d[y ] = Math . max(d[y ], v);
97
+ d[x ] = Math . max(d[x ], v);
149
98
}
150
99
return ans;
151
100
}
@@ -158,41 +107,17 @@ class Solution {
158
107
class Solution {
159
108
public:
160
109
int maximumSum(vector<int >& nums) {
161
- vector<vector<int >> d(100);
162
- for (int& v : nums) {
163
- int y = 0;
164
- for (int x = v; x > 0; x /= 10) {
165
- y += x % 10;
166
- }
167
- d[ y] .emplace_back(v);
168
- }
169
- int ans = -1;
170
- for (auto& vs : d) {
171
- if (vs.size() > 1) {
172
- sort(vs.rbegin(), vs.rend());
173
- ans = max(ans, vs[ 0] + vs[ 1] );
174
- }
175
- }
176
- return ans;
177
- }
178
- };
179
- ```
180
-
181
- ```cpp
182
- class Solution {
183
- public:
184
- int maximumSum(vector<int>& nums) {
185
- int ans = -1;
186
110
int d[ 100] {};
187
- for (int& v : nums) {
188
- int y = 0;
189
- for (int x = v; x; x /= 10) {
190
- y += x % 10;
111
+ int ans = -1;
112
+ for (int v : nums) {
113
+ int x = 0;
114
+ for (int y = v; y; y /= 10) {
115
+ x += y % 10;
191
116
}
192
- if (d[y ]) {
193
- ans = max(ans, d[y ] + v);
117
+ if (d[ x ] ) {
118
+ ans = max(ans, d[ x ] + v);
194
119
}
195
- d[y ] = max(d[y ], v);
120
+ d[ x ] = max(d[ x ] , v);
196
121
}
197
122
return ans;
198
123
}
@@ -203,39 +128,17 @@ public:
203
128
204
129
```go
205
130
func maximumSum(nums []int) int {
206
- d := [100 ][]int {}
207
- for _ , v := range nums {
208
- y := 0
209
- for x := v; x > 0 ; x /= 10 {
210
- y += x % 10
211
- }
212
- d[y] = append (d[y], v)
213
- }
214
- ans := -1
215
- for _ , vs := range d {
216
- m := len (vs)
217
- if m > 1 {
218
- sort.Ints (vs)
219
- ans = max (ans, vs[m-1 ]+vs[m-2 ])
220
- }
221
- }
222
- return ans
223
- }
224
- ```
225
-
226
- ``` go
227
- func maximumSum (nums []int ) int {
228
- ans := -1
229
131
d := [100]int{}
132
+ ans := -1
230
133
for _, v := range nums {
231
- y := 0
232
- for x := v; x > 0 ; x /= 10 {
233
- y += x % 10
134
+ x := 0
135
+ for y := v; y > 0; y /= 10 {
136
+ x += y % 10
234
137
}
235
- if d[y ] > 0 {
236
- ans = max (ans, d[y ]+v)
138
+ if d[x ] > 0 {
139
+ ans = max(ans, d[x ]+v)
237
140
}
238
- d[y ] = max (d[y ], v)
141
+ d[x ] = max(d[x ], v)
239
142
}
240
143
return ans
241
144
}
@@ -244,7 +147,47 @@ func maximumSum(nums []int) int {
244
147
### ** TypeScript**
245
148
246
149
``` ts
150
+ function maximumSum(nums : number []): number {
151
+ const d: number [] = Array (100 ).fill (0 );
152
+ let ans = - 1 ;
153
+ for (const v of nums ) {
154
+ let x = 0 ;
155
+ for (let y = v ; y ; y = (y / 10 ) | 0 ) {
156
+ x += y % 10 ;
157
+ }
158
+ if (d [x ]) {
159
+ ans = Math .max (ans , d [x ] + v );
160
+ }
161
+ d [x ] = Math .max (d [x ], v );
162
+ }
163
+ return ans ;
164
+ }
165
+ ```
166
+
167
+ ### ** Rust**
168
+
169
+ ``` rust
170
+ impl Solution {
171
+ pub fn maximum_sum (nums : Vec <i32 >) -> i32 {
172
+ let mut d = vec! [0 ; 100 ];
173
+ let mut ans = - 1 ;
247
174
175
+ for & v in nums . iter () {
176
+ let mut x : usize = 0 ;
177
+ let mut y = v ;
178
+ while y > 0 {
179
+ x += (y % 10 ) as usize ;
180
+ y /= 10 ;
181
+ }
182
+ if d [x ] > 0 {
183
+ ans = ans . max (d [x ] + v );
184
+ }
185
+ d [x ] = d [x ]. max (v );
186
+ }
187
+
188
+ ans
189
+ }
190
+ }
248
191
```
249
192
250
193
### ** ...**
0 commit comments