2918. Minimum Equal Sum of Two Arrays After Replacing Zeros

T: O(n)
S: O(1)
```java
class Solution {
    public long minSum(int[] nums1, int[] nums2) {
        long sum1 = 0;
        long zero1 = 0;
        long sum2 = 0;
        long zero2 = 0;
        for (int num : nums1) {
            sum1 += num;
            if (num == 0) {
                zero1++;
            }
        }
        for (int num : nums2) {
            sum2 += num;
            if (num == 0) {
                zero2++;
            }
        }
        // System.out.println("sum1:" + sum1 + ", zero1:" + zero1);
        // System.out.println("sum2:" + sum2 + ", zero2:" + zero2);
        if (zero2 == 0 && sum1+zero1 > sum2) {
            return -1;
        }
        if (zero1 == 0 && sum2+zero2 > sum1) {
            return -1;
        }
        return Math.max(sum1+zero1, sum2+zero2);
    }
}
/**
sum them

[20,0,18,11,0,0,0,0,0,0,17,28,0,11,10,0,0,15,29]
[16,9,25,16,1,9,20,28,8,0,1,0,1,27]

sum1:159, zero1:10
sum2:161, zero2:2
      2          8 


      169 - 161)/2 = 4


sum1:159, zero1:2
sum2:161, zero2:2

163 - 161 = 2

sum1:167, zero1:4
sum2:179, zero2:7


187 - 167 = 20


sum1:6, zero1:2
sum2:11, zero2:1


12 - 6) /2


focus on sum+zero is the bigger one, that's the best ans
T: O(n)
S: O(1)
*/
```

Last updated