3785. Minimum Swaps to Avoid Forbidden Values
Asked by 1 company
Topics
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 = 0andj = 1innumsand swap them, resulting innums = [2, 1, 3]. - After this swap, for every index
i,nums[i]is not equal toforbidden[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 = 0andj = 2innumsand swap them, resulting innums = [6, 6, 4, 5]. - Select indices
i = 1andj = 3innumsand swap them, resulting innums = [6, 5, 4, 6]. - After these swaps, for every index
i,nums[i]is not equal toforbidden[i].
Example 3:
Input: nums = [7,7], forbidden = [8,7]
Output: -1
Explanation:
It is not possible to makenums[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 <= 1051 <= nums[i], forbidden[i] <= 109
Hints
Hint 1
Hint 2
nums and forbidden into a map freq.Hint 3
freq[val] >= n + 1 return -1 (impossible).Hint 4
nums[i] == forbidden[i]) into a map badPairs[val] counts.Hint 5
badPairsSum be the sum of all bad counts and maxBadPairs the maximum bad count for any single value.Hint 6
max((badPairsSum + 1) / 2, maxBadPairs) (i.e. ceil(badPairsSum/2) vs the largest same-value bad cluster).