2918: Minimum-Equal-Sum-of-Two-Arrays-After-Replacing-Zeros
Medium
This question a fake medium or what?!
You can basically reduce the original question to:
Then, add on a follow-up question:
Finally, after answering this question:
This is because, we are allowed to change all the
0
s to ANY STRICTLY POSITIVE NUMBER,
meaning the minimum sum we can possibly find has to occur when all
0
s are swapped to 1
s.
Therefore that implies that if the smaller sum
has no 0
s present, it is simply
impossible to set both sums to be equal.
Code:
class Solution {
public:
long long minSum(vector<int>& nums1, vector<int>& nums2) {
long long sum1 = 0, sum2 = 0;
int count1 = 0, count2 = 0;
for (int& num : nums1) {
+= num;
sum1 if (num == 0) ++count1;
}
for (int& num : nums2) {
+= num;
sum2 if (num == 0) ++count2;
}
if (sum1 + count1 > sum2 + count2 && count2 == 0) {
return -1;
} else if (sum1 + count1 < sum2 + count2 && count1 == 0) {
return -1;
}
return max(sum1 + count1, sum2 + count2);
}
};