Minimum Swaps to Avoid Forbidden Values Problem
Description
LeetCode Problem 3785.
You are given two integer arrays, nums and forbidden, each of length n.
You may perform the following operation any number of times (including zero):
- Choose two distinct indices
iandj, and swapnums[i]withnums[j].
Return the minimum number of swaps required such that, for every index i, the value of nums[i] is not equal to forbidden[i]. If no amount of swaps can ensure that every index avoids its forbidden value, return -1.
Example 1:
Input: nums = [1,2,3], forbidden = [3,2,1]
Output: 1
Explanation:
One optimal set of swaps:
- Select indices i = 0 and j = 1 in nums and swap them, resulting in nums = [2, 1, 3].
- After this swap, for every index i, nums[i] is not equal to forbidden[i].
Example 2:
Input: nums = [4,6,6,5], forbidden = [4,6,5,5]
Output: 2
Explanation:
One optimal set of swaps:
- Select indices i = 0 and j = 2 in nums and swap them, resulting in nums = [6, 6, 4, 5].
- Select indices i = 1 and j = 3 in nums and swap them, resulting in nums = [6, 5, 4, 6].
- After these swaps, for every index i, nums[i] is not equal to forbidden[i].
Example 3:
Input: nums = [7,7], forbidden = [8,7]
Output: -1
Explanation:
It is not possible to make nums[i] different from forbidden[i] for all indices.
Example 4:
Input: nums = [1,2], forbidden = [2,1]
Output: 0
Explanation:
No swaps are required because nums[i] is already different from forbidden[i] for all indices, so the answer is 0.
Constraints:
1 <= n == nums.length == forbidden.length <= 10^51 <= nums[i], forbidden[i] <= 10^9
Solution
Approach: Count the Conflicts
Call index i a conflict if nums[i] == forbidden[i]. Only conflicts need fixing, and a swap can fix at most two of them.
Impossible case. A value v takes up one slot of nums for every occurrence in nums, and no position that has v in forbidden may hold it. If v appears in nums and forbidden more than n times in total, there are not enough positions left for all its copies, so the answer is -1. (A conflict counts twice, once in each array.) Otherwise a solution exists.
Counting swaps. Let k be the number of conflicts and most the largest number of conflicts that share the same value.
- Two conflicts with different values can be swapped with each other, fixing both. So with no dominant value,
ceil(k / 2)swaps are enough. - Conflicts with the same value cannot fix each other. If one value has
mostconflicts andmost > ceil(k / 2), each of them needs a swap with a position outside the group, which takesmostswaps.
The answer is max(ceil(k / 2), most).
Complexity: O(n) time and O(n) extra space for the hash tables.
from collections import Counter
from typing import List
class Solution:
def minSwaps(self, nums: List[int], forbidden: List[int]) -> int:
n = len(nums)
total = Counter()
conflict = Counter()
for x, y in zip(nums, forbidden):
total[x] += 1
total[y] += 1
if x == y:
conflict[x] += 1
# a value that fills more than n slots (as a number or as a ban) cannot be placed
if any(v > n for v in total.values()):
return -1
k = sum(conflict.values())
most = max(conflict.values(), default=0)
return max((k + 1) // 2, most)