LeetCode 题解工作台

通过最少操作次数使数组的和相等

给你两个长度可能不等的整数数组 nums1 和 nums2 。两个数组中的所有值都在 1 到 6 之间(包含 1 和 6 )。 每次操作中,你可以选择 任意 数组中的任意一个整数,将它变成 1 到 6 之间 任意 的值(包含 1 和 6 )。 请你返回使 nums1 中所有数的和与 nums2 中所…

category

4

题型

code_blocks

4

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·哈希·扫描

bolt

答案摘要

我们用 和 分别表示数组 `nums1` 和 `nums2` 的和。 如果 $s_1 = s_2$,则不需要任何操作,直接返回 。否则,我们不妨设 $s_1 \lt s_2$,即 中的元素和小于 中的元素和,那么两个数组元素和的差值 。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你两个长度可能不等的整数数组 nums1 和 nums2 。两个数组中的所有值都在 1 到 6 之间(包含 1 和 6)。

每次操作中,你可以选择 任意 数组中的任意一个整数,将它变成 1 到 6 之间 任意 的值(包含 1 和 6)。

请你返回使 nums1 中所有数的和与 nums2 中所有数的和相等的最少操作次数。如果无法使两个数组的和相等,请返回 -1 。

 

示例 1:

输入:nums1 = [1,2,3,4,5,6], nums2 = [1,1,2,2,2,2]
输出:3
解释:你可以通过 3 次操作使 nums1 中所有数的和与 nums2 中所有数的和相等。以下数组下标都从 0 开始。
- 将 nums2[0] 变为 6 。 nums1 = [1,2,3,4,5,6], nums2 = [6,1,2,2,2,2] 。
- 将 nums1[5] 变为 1 。 nums1 = [1,2,3,4,5,1], nums2 = [6,1,2,2,2,2] 。
- 将 nums1[2] 变为 2 。 nums1 = [1,2,2,4,5,1], nums2 = [6,1,2,2,2,2] 。

示例 2:

输入:nums1 = [1,1,1,1,1,1,1], nums2 = [6]
输出:-1
解释:没有办法减少 nums1 的和或者增加 nums2 的和使二者相等。

示例 3:

输入:nums1 = [6,6], nums2 = [1]
输出:3
解释:你可以通过 3 次操作使 nums1 中所有数的和与 nums2 中所有数的和相等。以下数组下标都从 0 开始。
- 将 nums1[0] 变为 2 。 nums1 = [2,6], nums2 = [1] 。
- 将 nums1[1] 变为 2 。 nums1 = [2,2], nums2 = [1] 。
- 将 nums2[0] 变为 4 。 nums1 = [2,2], nums2 = [4] 。

 

提示:

  • 1 <= nums1.length, nums2.length <= 105
  • 1 <= nums1[i], nums2[i] <= 6
lightbulb

解题思路

方法一:贪心 + 排序

我们用 s1s_1s2s_2 分别表示数组 nums1nums2 的和。

如果 s1=s2s_1 = s_2,则不需要任何操作,直接返回 00。否则,我们不妨设 s1<s2s_1 \lt s_2,即 nums1nums_1 中的元素和小于 nums2nums_2 中的元素和,那么两个数组元素和的差值 d=s2s1d=s_2-s_1

要使得两个数组元素和相等,我们需要对 nums1 中的元素进行增大操作,对 nums2 中的元素进行减小操作。

对于 nums1 中的每个元素 vv,我们最多可以将其增大到 66,那么 vv 可以增大的量为 6v6-v。对于 nums2 中的每个元素 vv,我们最多可以将其减小到 11,那么 vv 可以减小的量为 v1v-1

我们将每个元素的变化量放入数组 arr 中,然后对数组 arr 进行降序排列。

接下来,我们从数组 arr 的第一个元素开始,贪心地将 dd 减去每个元素的变化量,直到 d0d \leq 0,返回此时的操作次数即可。

遍历结束后,如果 d>0d \gt 0,说明无法使得两个数组元素和相等,返回 1-1

时间复杂度 O((m+n)×log(m+n))O((m+n) \times \log (m + n)),空间复杂度 O(m+n)O(m+n)。其中 mmnn 分别为数组 nums1nums2 的长度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution:
    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:
        s1, s2 = sum(nums1), sum(nums2)
        if s1 == s2:
            return 0
        if s1 > s2:
            return self.minOperations(nums2, nums1)
        arr = [6 - v for v in nums1] + [v - 1 for v in nums2]
        d = s2 - s1
        for i, v in enumerate(sorted(arr, reverse=True), 1):
            d -= v
            if d <= 0:
                return i
        return -1
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Evaluate how efficiently the candidate uses hash tables to track frequencies of array elements.

  • question_mark

    Check the candidate's ability to apply a greedy approach for minimal operations.

  • question_mark

    Assess if the candidate considers edge cases where the sums cannot be equalized.

warning

常见陷阱

外企场景
  • error

    Not considering cases where it is impossible to balance the sums, which should return -1.

  • error

    Using an inefficient method to find the best elements to modify, resulting in higher-than-necessary operation counts.

  • error

    Neglecting to check if the arrays' sums are already equal before proceeding with operations.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    What if the arrays have significantly larger sizes or different value ranges?

  • arrow_right_alt

    How would the solution change if the value range of elements was increased beyond 1 to 6?

  • arrow_right_alt

    Can this problem be solved by minimizing operations in a different order or sequence of changes?

help

常见问题

外企场景

通过最少操作次数使数组的和相等题解:数组·哈希·扫描 | LeetCode #1775 中等