Skip to content

Commit f9dac0f

Browse files
committed
Sync LeetCode submission Runtime - 884 ms (17.25%), Memory - 34.8 MB (66.16%)
1 parent 480bf58 commit f9dac0f

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<p>You are given two arrays <code>nums1</code> and <code>nums2</code> consisting of positive integers.</p>
2+
3+
<p>You have to replace <strong>all</strong> the <code>0</code>&#39;s in both arrays with <strong>strictly</strong> positive integers such that the sum of elements of both arrays becomes <strong>equal</strong>.</p>
4+
5+
<p>Return <em>the <strong>minimum</strong> equal sum you can obtain, or </em><code>-1</code><em> if it is impossible</em>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> nums1 = [3,2,0,1,0], nums2 = [6,5,0]
12+
<strong>Output:</strong> 12
13+
<strong>Explanation:</strong> We can replace 0&#39;s in the following way:
14+
- Replace the two 0&#39;s in nums1 with the values 2 and 4. The resulting array is nums1 = [3,2,2,1,4].
15+
- Replace the 0 in nums2 with the value 1. The resulting array is nums2 = [6,5,1].
16+
Both arrays have an equal sum of 12. It can be shown that it is the minimum sum we can obtain.
17+
</pre>
18+
19+
<p><strong class="example">Example 2:</strong></p>
20+
21+
<pre>
22+
<strong>Input:</strong> nums1 = [2,0,2,0], nums2 = [1,4]
23+
<strong>Output:</strong> -1
24+
<strong>Explanation:</strong> It is impossible to make the sum of both arrays equal.
25+
</pre>
26+
27+
<p>&nbsp;</p>
28+
<p><strong>Constraints:</strong></p>
29+
30+
<ul>
31+
<li><code>1 &lt;= nums1.length, nums2.length &lt;= 10<sup>5</sup></code></li>
32+
<li><code>0 &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li>
33+
</ul>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# n = len(nums1), m = len(nums2)
2+
# Time: O(n + m)
3+
# Space: O(1)
4+
5+
class Solution:
6+
def minSum(self, nums1: List[int], nums2: List[int]) -> int:
7+
sum1 = sum2 = 0
8+
zero1 = zero2 = 0
9+
10+
for i in nums1:
11+
sum1 += i
12+
if i == 0:
13+
sum1 += 1
14+
zero1 += 1
15+
16+
for i in nums2:
17+
sum2 += i
18+
if i == 0:
19+
sum2 += 1
20+
zero2 += 1
21+
22+
if (zero1 == 0 and sum2 > sum1) or (zero2 == 0 and sum1 > sum2):
23+
return -1
24+
25+
return max(sum1, sum2)

0 commit comments

Comments
 (0)