Skip to content
LC-2541 Medium LeetCode

2541. Minimum Operations to Make Array Equal II

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 32% Topics: Array, Math, Greedy
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

import itertools


# greedy
class Solution(object):
    def minOperations(self, nums1, nums2, k):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :type k: int
        :rtype: int
        """
        cnt1 = cnt2 = 0
        for x, y in itertools.izip(nums1, nums2):
            if y == x:
                continue
            if k == 0 or (y-x)%k:
                return -1
            if x < y:
                cnt1 += (y-x)//k
            else:
                cnt2 += (x-y)//k
        return cnt1 if cnt1 == cnt2 else -1

Solution from kamyu104/LeetCode-Solutions · MIT