2918.数组的最小相等和:if-else

【LetMeFly】2918.数组的最小相等和:if-else

力扣题目链接:https://leetcode.cn/problems/minimum-equal-sum-of-two-arrays-after-replacing-zeros/

给你两个由正整数和 0 组成的数组 nums1nums2

你必须将两个数组中的 所有 0 替换为 严格 正整数,并且满足两个数组中所有元素的和 相等

返回 最小 相等和 ,如果无法使两数组相等,则返回 -1

 

示例 1:

输入:nums1 = [3,2,0,1,0], nums2 = [6,5,0]
输出:12
解释:可以按下述方式替换数组中的 0 :
- 用 2 和 4 替换 nums1 中的两个 0 。得到 nums1 = [3,2,2,1,4] 。
- 用 1 替换 nums2 中的一个 0 。得到 nums2 = [6,5,1] 。
两个数组的元素和相等,都等于 12 。可以证明这是可以获得的最小相等和。

示例 2:

输入:nums1 = [2,0,2,0], nums2 = [1,4]
输出:-1
解释:无法使两个数组的和相等。

 

提示:

  • 1 <= nums1.length, nums2.length <= 105
  • 0 <= nums1[i], nums2[i] <= 106

解题方法:讨论

假设$nums1$的和为$s1$且$nums1$中有$c1$个$0$,$nums2$的和以及零的个数分别为$s2$和$c2$,则有:

  • 若$s1 < s2 + c2 and c1 == 0$,说明$nums2$的和至少变为$s2+c2$,比$s1$大并且$s1$中没有$0$可以使其和变大,直接返回$-1$;
  • 若$s1 + c1 > s2 and cs == 0$,同理,直接返回$-1$;
  • 否则,返回$max(s1 + c1, s2 + c2)$。

时空复杂度分析:

  • 时间复杂度$O(len(nums1) + len(nums2))$
  • 空间复杂度$O(1)$

AC代码

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
* @Author: LetMeFly
* @Date: 2025-05-10 12:07:54
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2025-05-10 19:00:36
*/
typedef long long ll;

class Solution {
public:
long long minSum(vector<int>& nums1, vector<int>& nums2) {
ll s1 = 0, s2 = 0;
int c1 = 0, c2 = 0;
for (int t : nums1) {
s1 += t;
c1 += t == 0;
}
for (int t : nums2) {
s2 += t;
c2 += t == 0;
}
if (s1 < s2 + c2 && c1 == 0 || s1 + c1 > s2 && c2 == 0) {
return -1;
}
return max(s1 + c1, s2 + c2);
}
};

Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
'''
Author: LetMeFly
Date: 2025-05-10 12:07:54
LastEditors: LetMeFly.xyz
LastEditTime: 2025-05-10 19:29:26
'''
from typing import List, Tuple

class Solution:
def cal1(self, nums: List[int]) -> Tuple[int, int]:
s, c = 0, 0
for t in nums:
if t:
s += t
else:
c += 1
return s, c

def minSum(self, nums1: List[int], nums2: List[int]) -> int:
s1, c1 = self.cal1(nums1)
s2, c2 = self.cal1(nums2)
if s1 < s2 + c2 and c1 == 0 or s1 + c1 > s2 and c2 == 0:
return -1
return max(s1 + c1, s2 + c2)

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
* @Author: LetMeFly
* @Date: 2025-05-10 12:07:54
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2025-05-10 19:32:19
*/
class Solution {
public long minSum(int[] nums1, int[] nums2) {
long s1 = 0, s2 = 0;
int c1 = 0, c2 = 0;
for (int t : nums1) {
s1 += t;
if (t == 0) {
c1++;
}
}
for (int t : nums2) {
s2 += t;
if (t == 0) {
c2++;
}
}
if (s1 < s2 + c2 && c1 == 0 || s1 + c1 > s2 && c2 == 0) {
return -1;
}
return Math.max(s1 + c1, s2 + c2);
}
}

Go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
* @Author: LetMeFly
* @Date: 2025-05-10 12:07:54
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2025-05-10 19:38:55
*/
package main

func cal2918(a []int) (s int64, cnt int64) {
for _, t := range a {
if (t == 0) {
cnt++
} else {
s += int64(t)
}
}
return
}

func minSum(nums1 []int, nums2 []int) int64 {
s1, c1 := cal2918(nums1)
s2, c2 := cal2918(nums2)
if s1 < s2 + c2 && c1 == 0 || s1 + c1 > s2 && c2 == 0 {
return -1
}
return max(s1 + c1, s2 + c2)
}

同步发文于CSDN和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~

千篇源码题解已开源


2918.数组的最小相等和:if-else
https://blog.letmefly.xyz/2025/05/10/LeetCode 2918.数组的最小相等和/
作者
发布于
2025年5月10日
许可协议