Given two integer arrays nums1 and nums2 and an integer k, find the minimum number of deletions required in nums2 so that it has no overlap with the first k elements of nums1.
A deletion removes one element from nums2. Return the minimum number of deletions needed so that no element of the modified nums2 appears among the first k elements of nums1.
Function Signature
def min_deletions(nums1: list[int], nums2: list[int], k: int) -> int:
Rules
-
The first
k
elements of
nums1
are
nums1[0]
through
nums1[k - 1]
. A value that appears only later in
nums1
is allowed in
nums2
.
-
Repeated values in
nums2
count separately. If a forbidden value appears three times in
nums2
, all three copies must be deleted.
-
Repeated values inside the first
k
elements of
nums1
change nothing; only which values appear there matters.
Constraints
-
1 <= nums1.length <= 10^4
-
1 <= nums2.length <= 10^4
-
1 <= k <= nums1.length
-
-10^9 <= nums1[i], nums2[i] <= 10^9
Examples
Input: nums1 = [1,2,3,4,5], nums2 = [3,4,5,6,7], k = 3
Output: 1
The first 3 elements of nums1 are 1, 2 and 3. Only the 3 in nums2 is one of them. The 4 and 5 also appear in nums1, but after the first 3 positions, so they stay.
Input: nums1 = [10,20,30], nums2 = [10,20,30,40], k = 2
Output: 2
The forbidden values are 10 and 20, and both appear in nums2, so both are deleted. The 30 is in nums1 but outside its first 2 elements.
Input: nums1 = [5,5,8,2], nums2 = [5,2,5,8,9], k = 2
Output: 2
The first 2 elements of nums1 are both 5, so 5 is the only forbidden value. It appears twice in nums2, and each copy costs one deletion. The 2 and 8 come later in nums1 and stay.