aboutsummaryrefslogtreecommitdiff
path: root/min_equal_sum_two_arr.py
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-05-10 00:07:51 -0600
committerJack Sangdahl <[email protected]>2025-05-10 00:07:51 -0600
commit42c1b2150db0dfed4e106379f8ad6dc966bb3c2e (patch)
treea3db189f2eaccf73977cbfcd8238d4bf8fc3e4ca /min_equal_sum_two_arr.py
parent67d6c0ec318b5e9726ce680cb17df94e55510ba2 (diff)
downloadleetcode-42c1b2150db0dfed4e106379f8ad6dc966bb3c2e.tar.gz
leetcode-42c1b2150db0dfed4e106379f8ad6dc966bb3c2e.zip
2918. minimum equal sum of two arrys after replacing zeros
Diffstat (limited to 'min_equal_sum_two_arr.py')
-rw-r--r--min_equal_sum_two_arr.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/min_equal_sum_two_arr.py b/min_equal_sum_two_arr.py
new file mode 100644
index 0000000..a552b0e
--- /dev/null
+++ b/min_equal_sum_two_arr.py
@@ -0,0 +1,30 @@
+# 2918. Minimum Equal Sum of Two Arrays After Replacing Zeros
+
+"""
+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.
+"""
+
+
+class Solution(object):
+ def minSum(self, nums1, nums2):
+ """
+ :type nums1: List[int]
+ :type nums2: List[int]
+ :rtype: int
+ """
+ sum1 = sum(max(i, 1) for i in nums1)
+ sum2 = sum(max(i, 1) for i in nums2)
+ if sum1 < sum2 and nums1.count(0) == 0:
+ return -1
+ if sum1 > sum2 and nums2.count(0) == 0:
+ return -1
+ return max(sum1, sum2)
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.minSum(nums1=[3, 2, 0, 1, 0], nums2=[6, 5, 0]))
+ print(obj.minSum(nums1=[2, 0, 2, 0], nums2=[1, 4]))