返回介绍

solution / 2900-2999 / 2918.Minimum Equal Sum of Two Arrays After Replacing Zeros / README_EN

发布于 2024-06-17 01:02:58 字数 5289 浏览 0 评论 0 收藏 0

2918. Minimum Equal Sum of Two Arrays After Replacing Zeros

中文文档

Description

You are given two arrays nums1 and nums2 consisting of positive integers.

You have to replace all the 0's in both arrays with strictly positive integers such that the sum of elements of both arrays becomes equal.

Return _the minimum equal sum you can obtain, or _-1_ if it is impossible_.

 

Example 1:

Input: nums1 = [3,2,0,1,0], nums2 = [6,5,0]
Output: 12
Explanation: We can replace 0's in the following way:
- Replace the two 0's in nums1 with the values 2 and 4. The resulting array is nums1 = [3,2,2,1,4].
- Replace the 0 in nums2 with the value 1. The resulting array is nums2 = [6,5,1].
Both arrays have an equal sum of 12. It can be shown that it is the minimum sum we can obtain.

Example 2:

Input: nums1 = [2,0,2,0], nums2 = [1,4]
Output: -1
Explanation: It is impossible to make the sum of both arrays equal.

 

Constraints:

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

Solutions

Solution 1: Case Analysis

We consider the case where we treat all $0$s in the array as $1$s, and calculate the sum of the two arrays separately, denoted as $s_1$ and $s_2$. Without loss of generality, we assume that $s_1 \le s_2$.

  • If $s_1 = s_2$, then the answer is $s_1$.
  • If $s_1 \lt s_2$, then there must exist a $0$ in $nums1$ to make the sum of the two arrays equal. In this case, the answer is $s_2$. Otherwise, it means that it is impossible to make the sum of the two arrays equal, and we return $-1$.

The time complexity is $O(n + m)$, where $n$ and $m$ are the lengths of the arrays $nums1$ and $nums2$, respectively. The space complexity is $O(1)$.

class Solution:
  def minSum(self, nums1: List[int], nums2: List[int]) -> int:
    s1 = sum(nums1) + nums1.count(0)
    s2 = sum(nums2) + nums2.count(0)
    if s1 > s2:
      return self.minSum(nums2, nums1)
    if s1 == s2:
      return s1
    return -1 if nums1.count(0) == 0 else s2
class Solution {
  public long minSum(int[] nums1, int[] nums2) {
    long s1 = 0, s2 = 0;
    boolean hasZero = false;
    for (int x : nums1) {
      hasZero |= x == 0;
      s1 += Math.max(x, 1);
    }
    for (int x : nums2) {
      s2 += Math.max(x, 1);
    }
    if (s1 > s2) {
      return minSum(nums2, nums1);
    }
    if (s1 == s2) {
      return s1;
    }
    return hasZero ? s2 : -1;
  }
}
class Solution {
public:
  long long minSum(vector<int>& nums1, vector<int>& nums2) {
    long long s1 = 0, s2 = 0;
    bool hasZero = false;
    for (int x : nums1) {
      hasZero |= x == 0;
      s1 += max(x, 1);
    }
    for (int x : nums2) {
      s2 += max(x, 1);
    }
    if (s1 > s2) {
      return minSum(nums2, nums1);
    }
    if (s1 == s2) {
      return s1;
    }
    return hasZero ? s2 : -1;
  }
};
func minSum(nums1 []int, nums2 []int) int64 {
  s1, s2 := 0, 0
  hasZero := false
  for _, x := range nums1 {
    if x == 0 {
      hasZero = true
    }
    s1 += max(x, 1)
  }
  for _, x := range nums2 {
    s2 += max(x, 1)
  }
  if s1 > s2 {
    return minSum(nums2, nums1)
  }
  if s1 == s2 {
    return int64(s1)
  }
  if hasZero {
    return int64(s2)
  }
  return -1
}
function minSum(nums1: number[], nums2: number[]): number {
  let [s1, s2] = [0, 0];
  let hasZero = false;
  for (const x of nums1) {
    if (x === 0) {
      hasZero = true;
    }
    s1 += Math.max(x, 1);
  }
  for (const x of nums2) {
    s2 += Math.max(x, 1);
  }
  if (s1 > s2) {
    return minSum(nums2, nums1);
  }
  if (s1 === s2) {
    return s1;
  }
  return hasZero ? s2 : -1;
}
public class Solution {
  public long MinSum(int[] nums1, int[] nums2) {
    long s1 = 0, s2 = 0;
    bool hasZero = false;
    foreach (int x in nums1) {
      hasZero |= x == 0;
      s1 += Math.Max(x, 1);
    }
    foreach (int x in nums2) {
      s2 += Math.Max(x, 1);
    }
    if (s1 > s2) {
      return MinSum(nums2, nums1);
    }
    if (s1 == s2) {
      return s1;
    }
    return hasZero ? s2 : -1;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文