Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: add solutions to lc problem: No.2342 #1979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
acbin merged 1 commit into main from dev
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,17 @@

<!-- 这里可写通用的实现逻辑 -->

**方法一:哈希表 + 排序**
**方法一:哈希表**

对于数组中的每个元素,计算其数位和,将其存入哈希表 $d$ 中,哈希表的键为数位和,值为数组中所有数位和为该键的元素组成的数组
我们可以用一个哈希表 $d$ 记录每个数位和对应的最大值,初始化一个答案变量 $ans = -1$

遍历哈希表 $d,ドル对于每个键值对,如果该键对应的数组长度大于 1ドル,ドル则对该数组进行降序排序,取前两个元素相加,更新答案
接下来,我们遍历数组 $nums,ドル对于每个数 $v,ドル我们计算它的数位和 $x,ドル如果 $x$ 在哈希表 $d$ 中存在,那么我们就更新答案 $ans = \max(ans, d[x] + v)$。然后更新哈希表 $d[x] = \max(d[x], v)$

最终返回答案
最后返回答案 $ans$ 即可

时间复杂度 $O(n \times \log n),ドル空间复杂度 $O(n)$。其中 $n$ 为数组 `nums` 的长度
由于 $nums$ 中的元素最大为 10ドル^9,ドル因此数位和最大为 9ドル \times 9 = 81,ドル我们可以直接定义一个长度为 100ドル$ 的数组 $d$ 来代替哈希表

**方法二:哈希表(优化)**

我们创建一个哈希表 $d,ドル其中哈希表的键为数位和,值为已遍历过的元素中数位和为该键的最大元素。

我们直接对数组 `nums` 进行遍历,对于每个元素 $v,ドル计算其数位和 $y,ドル如果 $d[y]$ 存在,则更新答案为 $max(ans, v + d[y])$。然后我们更新 $d[y]=max(d[y], v)$。

最终返回答案。

时间复杂度 $O(n),ドル空间复杂度 $O(C)$。其中 $n$ 为数组 `nums` 的长度,而 $C$ 为数组 `nums` 的最大数位和。本题中 $nums[i] \leq 10^9,ドル因此我们固定取 $C=100$ 即可。
时间复杂度 $O(n \times \log M),ドル空间复杂度 $O(D),ドル其中 $n$ 是数组 $nums$ 的长度,而 $M$ 和 $D$ 分别是数组 $nums$ 中的元素的最大值和数位和的最大值。本题中 $M \leq 10^9,ドル$D \leq 81$。

<!-- tabs:start -->

Expand All @@ -72,34 +64,16 @@
```python
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
```

```python
class Solution:
def maximumSum(self, nums: List[int]) -> int:
ans = -1
d = defaultdict(int)
ans = -1
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)
x, y = 0, v
while y:
x += y % 10
y //= 10
if x in d:
ans = max(ans, d[x] + v)
d[x] = max(d[x], v)
return ans
```

Expand All @@ -110,42 +84,17 @@ class Solution:
```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;
}
}
```

```java
class Solution {
public int maximumSum(int[] nums) {
int ans = -1;
int[] d = new int[100];
int ans = -1;
for (int v : nums) {
int y = 0;
for (int x = v; x > 0; x /= 10) {
y += x % 10;
int x = 0;
for (int y = v; y > 0; y /= 10) {
x += y % 10;
}
if (d[y] > 0) {
ans = Math.max(ans, d[y] + v);
if (d[x] > 0) {
ans = Math.max(ans, d[x] + v);
}
d[y] = Math.max(d[y], v);
d[x] = Math.max(d[x], v);
}
return ans;
}
Expand All @@ -158,41 +107,17 @@ class Solution {
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;
}
};
```

```cpp
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;
int ans = -1;
for (int v : nums) {
int x = 0;
for (int y = v; y; y /= 10) {
x += y % 10;
}
if (d[y]) {
ans = max(ans, d[y] + v);
if (d[x]) {
ans = max(ans, d[x] + v);
}
d[y] = max(d[y], v);
d[x] = max(d[x], v);
}
return ans;
}
Expand All @@ -203,39 +128,17 @@ public:

```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
}
```

```go
func maximumSum(nums []int) int {
ans := -1
d := [100]int{}
ans := -1
for _, v := range nums {
y := 0
for x := v; x > 0; x /= 10 {
y += x % 10
x := 0
for y := v; y > 0; y /= 10 {
x += y % 10
}
if d[y] > 0 {
ans = max(ans, d[y]+v)
if d[x] > 0 {
ans = max(ans, d[x]+v)
}
d[y] = max(d[y], v)
d[x] = max(d[x], v)
}
return ans
}
Expand All @@ -244,7 +147,47 @@ func maximumSum(nums []int) int {
### **TypeScript**

```ts
function maximumSum(nums: number[]): number {
const d: number[] = Array(100).fill(0);
let ans = -1;
for (const v of nums) {
let x = 0;
for (let y = v; y; y = (y / 10) | 0) {
x += y % 10;
}
if (d[x]) {
ans = Math.max(ans, d[x] + v);
}
d[x] = Math.max(d[x], v);
}
return ans;
}
```

### **Rust**

```rust
impl Solution {
pub fn maximum_sum(nums: Vec<i32>) -> i32 {
let mut d = vec![0; 100];
let mut ans = -1;

for &v in nums.iter() {
let mut x: usize = 0;
let mut y = v;
while y > 0 {
x += (y % 10) as usize;
y /= 10;
}
if d[x] > 0 {
ans = ans.max(d[x] + v);
}
d[x] = d[x].max(v);
}

ans
}
}
```

### **...**
Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /