-
Notifications
You must be signed in to change notification settings - Fork 1
/
2540.minimum-common-value.py
69 lines (66 loc) · 1.55 KB
/
2540.minimum-common-value.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#
# @lc app=leetcode id=2540 lang=python3
#
# [2540] Minimum Common Value
#
# https://leetcode.com/problems/minimum-common-value/description/
#
# algorithms
# Easy (51.62%)
# Likes: 310
# Dislikes: 4
# Total Accepted: 32.1K
# Total Submissions: 62.5K
# Testcase Example: '[1,2,3]\n[2,4]'
#
# 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.
#
#
# Example 1:
#
#
# Input: nums1 = [1,2,3], nums2 = [2,4]
# Output: 2
# Explanation: The smallest element common to both arrays is 2, so we return
# 2.
#
#
# Example 2:
#
#
# Input: nums1 = [1,2,3,6], nums2 = [2,3,4,5]
# Output: 2
# Explanation: There are two common elements in the array 2 and 3 out of which
# 2 is the smallest, so 2 is returned.
#
#
#
# Constraints:
#
#
# 1 <= nums1.length, nums2.length <= 10^5
# 1 <= nums1[i], nums2[j] <= 10^9
# Both nums1 and nums2 are sorted in non-decreasing order.
#
#
#
# @lc code=start
class Solution:
def getCommon(self, nums1: List[int], nums2: List[int]) -> int:
p1, p2 = 0, 0
n1 = len(nums1)
n2 = len(nums2)
while p1 < n1 and p2 < n2:
if nums1[p1] == nums2[p2]:
return nums1[p1]
elif nums1[p1] < nums2[p2]:
p1 += 1
else:
p2 += 1
return -1
# @lc code=end