{"id":10093,"date":"2023-10-29T20:08:22","date_gmt":"2023-10-30T03:08:22","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10093"},"modified":"2023-10-29T20:08:51","modified_gmt":"2023-10-30T03:08:51","slug":"leetcode-2918-minimum-equal-sum-of-two-arrays-after-replacing-zeros","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/uncategorized\/leetcode-2918-minimum-equal-sum-of-two-arrays-after-replacing-zeros\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2918. Minimum Equal Sum of Two Arrays After Replacing Zeros"},"content":{"rendered":"\n<p>You are given two arrays&nbsp;<code>nums1<\/code>&nbsp;and&nbsp;<code>nums2<\/code>&nbsp;consisting of positive integers.<\/p>\n\n\n\n<p>You have to replace&nbsp;<strong>all<\/strong>&nbsp;the&nbsp;<code>0<\/code>&#8216;s in both arrays with&nbsp;<strong>strictly<\/strong>&nbsp;positive integers such that the sum of elements of both arrays becomes&nbsp;<strong>equal<\/strong>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>minimum<\/strong>&nbsp;equal sum you can obtain, or&nbsp;<\/em><code>-1<\/code><em>&nbsp;if it is impossible<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums1 = [3,2,0,1,0], nums2 = [6,5,0]\n<strong>Output:<\/strong> 12\n<strong>Explanation:<\/strong> We can replace 0's in the following way:\n- Replace the two 0's in nums1 with the values 2 and 4. The resulting array is nums1 = [3,2,2,1,4].\n- Replace the 0 in nums2 with the value 1. The resulting array is nums2 = [6,5,1].\nBoth arrays have an equal sum of 12. It can be shown that it is the minimum sum we can obtain.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums1 = [2,0,2,0], nums2 = [1,4]\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> It is impossible to make the sum of both arrays equal.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums1.length, nums2.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>0 &lt;= nums1[i], nums2[i] &lt;= 10<sup>6<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: A few cases<\/strong><\/h2>\n\n\n\n<p>Calculate the sum of number of zeros of each array as (s1, z1), (s2, z2). There are a few cases:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>z1 == z2 == 0, there is no way to change, just check s1 == s2.<\/li><li>z1 == 0, z1 != 0 or z2 == 0, z1 != 0. Need to at least increase the sum by number of zeros, check s1 + z1 &lt;= s2 \/ s2 + z2 &lt;= s1<\/li><li>z1 != 0, z2 != 0, the min sum is max(s1 + z1, s2 + z2)<\/li><\/ol>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  long long minSum(vector<int>& nums1, vector<int>& nums2) {\n    auto getSumAndZeros = [](const vector<int>& nums) {\n      long long sum = 0;\n      long long zeros = 0;\n      for (int x : nums) {\n        sum += x;\n        zeros += x == 0;\n      }\n      return pair{sum, zeros};\n    };\n    auto [s1, z1] = getSumAndZeros(nums1);\n    auto [s2, z2] = getSumAndZeros(nums2);\n    if (z1 == 0 && z2 == 0) {\n      return s1 == s2 ? s1 : -1;\n    } else if (z1 == 0) {\n      return s2 + z2 <= s1 ? s1 : -1;\n    } else if (z2 == 0) {\n      return s1 + z1 <= s2 ? s2 : -1;\n    } else {\n      return max(s1 + z1, s2 + z2);\n    }\n    return -1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two arrays&nbsp;nums1&nbsp;and&nbsp;nums2&nbsp;consisting of positive integers. You have to replace&nbsp;all&nbsp;the&nbsp;0&#8216;s in both arrays with&nbsp;strictly&nbsp;positive integers such that the sum of elements of both&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10093","post","type-post","status-publish","format-standard","hentry","category-uncategorized","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10093","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=10093"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10093\/revisions"}],"predecessor-version":[{"id":10095,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10093\/revisions\/10095"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10093"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}