Skip to content
LC-2449 Hard LeetCode

2449. Minimum Number of Operations to Make Arrays Similar

Read the full problem statement on LeetCode.
Difficulty: hard Acceptance: 60% Topics: Array, Greedy, Sorting
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(nlogn)
# Space: O(1)

import itertools


# greedy, sort
class Solution(object):
    def makeSimilar(self, nums, target):
        """
        :type nums: List[int]
        :type target: List[int]
        :rtype: int
        """
        nums.sort(key=lambda x: (x%2, x))
        target.sort(key=lambda x: (x%2, x))
        return sum(abs(x-y)//2 for x, y in itertools.izip(nums, target))//2

Solution from kamyu104/LeetCode-Solutions · MIT