2540. Minimum Common Value
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 58% Topics: Array, Hash Table, Two Pointers, Binary Search
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(1)
# two pointers
class Solution(object):
def getCommon(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: int
"""
i = j = 0
while i < len(nums1) and j < len(nums2):
if nums1[i] < nums2[j]:
i += 1
elif nums1[i] > nums2[j]:
j += 1
else:
return nums1[i]
return -1
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions