so thats y i say planning before coding is impt. I tried doing all the cases logic like if length1==length2 and if sum2>sum1 and zero2>zero1 stuff like that but actually the core logic is this.
wat if instead of trying to figure out if zero1>zero2 and stuff, we replace all zeros with 1 cuz the q is asking for the min sum anyway?
so doesnt matter the length, if there is just a single 0 in both lists, we can make a valid sum pair and answer is just max(sum1,sum2) considering we substituted all zeros with ones.
but if there is no 0 in either, lets say sum2>sum1 and there is no 0 in sum1 to increase our sum1 to sum2, then its invalid. Same for the other side.
For other cases, we can just return max(sum1,sum2)
if theres no zeros in both lists, the main if statement takes care of that
class Solution:
def minSum(self, nums1: List[int], nums2: List[int]) -> int:
length1,length2=len(nums1),len(nums2)
sum1,zero1=sum(max(1,i) for i in nums1),nums1.count(0)
sum2,zero2=sum(max(1,i) for i in nums2),nums2.count(0)
if sum2>sum1 and zero1==0 or sum1>sum2 and zero2==0:
return -1
return max(sum2,sum1)
n time
1 space