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

发布于:2025-05-13 ⋅ 阅读:(20) ⋅ 点赞:(0)

【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

解题方法:讨论

假设 n u m s 1 nums1 nums1的和为 s 1 s1 s1 n u m s 1 nums1 nums1中有 c 1 c1 c1 0 0 0 n u m s 2 nums2 nums2的和以及零的个数分别为 s 2 s2 s2 c 2 c2 c2,则有:

  • s 1 < s 2 + c 2 a n d c 1 = = 0 s1 < s2 + c2 and c1 == 0 s1<s2+c2andc1==0,说明 n u m s 2 nums2 nums2的和至少变为 s 2 + c 2 s2+c2 s2+c2,比 s 1 s1 s1大并且 s 1 s1 s1中没有 0 0 0可以使其和变大,直接返回 − 1 -1 1
  • s 1 + c 1 > s 2 a n d c s = = 0 s1 + c1 > s2 and cs == 0 s1+c1>s2andcs==0,同理,直接返回 − 1 -1 1
  • 否则,返回 m a x ( s 1 + c 1 , s 2 + c 2 ) max(s1 + c1, s2 + c2) max(s1+c1,s2+c2)

时空复杂度分析:

  • 时间复杂度 O ( l e n ( n u m s 1 ) + l e n ( n u m s 2 ) ) O(len(nums1) + len(nums2)) O(len(nums1)+len(nums2))
  • 空间复杂度 O ( 1 ) O(1) O(1)

AC代码

C++
/*
 * @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
'''
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
/*
 * @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
/*
 * @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和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~

千篇源码题解已开源