#2449
Minimum Number of Operations to Make Arrays Similar
expert · 1150 · lc hard +32 · verified · 61.5% accepted · 450 likes · top 62%
Description
Given two positive integer arrays nums and target of the same length n, you can swap any two elements within nums in one operation.
Return the minimum number of operations needed so that for every value v, nums and target contain the same frequency of v.
Example 1:
Input: nums = [8,12,6], target = [2,14,10]
Output: 2
Explanation: It is possible to make nums similar to target in two operations:
- Choose i = 0 and j = 2, nums = [10,12,4].
- Choose i = 1 and j = 2, nums = [10,14,2].
It can be shown that 2 is the minimum number of operations needed.
Example 2:
Input: nums = [1,2,5], target = [4,1,3]
Output: 1
Explanation: We can make nums similar to target in one operation:
- Choose i = 1 and j = 2, nums = [1,4,3].
Example 3:
Input: nums = [1,1,1,1,1], target = [1,1,1,1,1]
Output: 0
Explanation: The array nums is already similiar to target.
Code
1
2
3