Description: Given two integer arrays nums1 and nums2, sorted in non-decreasing order, return the minimum integer common to both arrays. If there is no common integer amongst nums1 and nums2, return -1.
Note that an integer is said to be common to nums1 and nums2 if both arrays have at least one occurrence of that integer.
Code:
def getCommon(self, nums1: List[int], nums2: List[int]) -> int:
i = j = 0
while i < len(nums1) and j < len(nums2):
if nums1[i] == nums2[j]:
return nums1[i]
elif nums1[i] < nums2[j]:
i += 1
else:
j += 1
return -1
Efficiency:
Time Complexity: O(m + n)
Space Complexity: O(1)
Test:
nums1 = [1,2,3]
nums2 = [2,4]
print(getCommon(s))#2
nums1 = [1,2,3,6]
nums2 = [2,3,4,5]
print(getCommon(s))#2