LeetCode 题解工作台

对数组执行操作使平方和最大

给你一个下标从 0 开始的整数数组 nums 和一个 正 整数 k 。 你可以对数组执行以下操作 任意次 : 选择两个互不相同的下标 i 和 j , 同时 将 nums[i] 更新为 (nums[i] AND nums[j]) 且将 nums[j] 更新为 (nums[i] OR nums[j]) …

category

4

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

困难 · 数组·哈希·扫描

bolt

答案摘要

根据题目描述,对于一个操作,我们可以将 变为 $nums[i] \textit{ AND } nums[j]$,将 变为 $nums[i] \textit{ OR } nums[j]$。我们不妨按位考虑,两个 或两个 进行这样的操作,结果都不会改变,如果是 和 进行这样的操作,结果会变成 和 ,也即是说,我们可以将 转移到 上,而 不会转移到 上。 因此,我们可以用一个数组 …

Interview AiBox logo

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

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个下标从 0 开始的整数数组 nums 和一个  整数 k 。

你可以对数组执行以下操作 任意次 :

  • 选择两个互不相同的下标 i 和 j ,同时 将 nums[i] 更新为 (nums[i] AND nums[j]) 且将 nums[j] 更新为 (nums[i] OR nums[j]) ,OR 表示按位  运算,AND 表示按位  运算。

你需要从最终的数组里选择 k 个元素,并计算它们的 平方 之和。

请你返回你可以得到的 最大 平方和。

由于答案可能会很大,将答案对 109 + 7 取余 后返回。

 

示例 1:

输入:nums = [2,6,5,8], k = 2
输出:261
解释:我们可以对数组执行以下操作:
- 选择 i = 0 和 j = 3 ,同时将 nums[0] 变为 (2 AND 8) = 0 且 nums[3] 变为 (2 OR 8) = 10 ,结果数组为 nums = [0,6,5,10] 。
- 选择 i = 2 和 j = 3 ,同时将 nums[2] 变为 (5 AND 10) = 0 且 nums[3] 变为 (5 OR 10) = 15 ,结果数组为 nums = [0,6,0,15] 。
从最终数组里选择元素 15 和 6 ,平方和为 152 + 62 = 261 。
261 是可以得到的最大结果。

示例 2:

输入:nums = [4,5,4,7], k = 3
输出:90
解释:不需要执行任何操作。
选择元素 7 ,5 和 4 ,平方和为 72 + 52 + 42 = 90 。
90 是可以得到的最大结果。

 

提示:

  • 1 <= k <= nums.length <= 105
  • 1 <= nums[i] <= 109
lightbulb

解题思路

方法一:位运算 + 贪心

根据题目描述,对于一个操作,我们可以将 nums[i]nums[i] 变为 nums[i] AND nums[j]nums[i] \textit{ AND } nums[j],将 nums[j]nums[j] 变为 nums[i] OR nums[j]nums[i] \textit{ OR } nums[j]。我们不妨按位考虑,两个 11 或两个 00 进行这样的操作,结果都不会改变,如果是 1100 进行这样的操作,结果会变成 0011,也即是说,我们可以将 11 转移到 00 上,而 00 不会转移到 11 上。

因此,我们可以用一个数组 cntcnt 统计每个位置上 11 的个数,然后从中选择 kk 个数。由于要使得平方和最大,每次选择的数要尽可能大。这是因为,假设两个数的平方和为 a2+b2a^2 + b^2(其中 a>ba \gt b),将两个数平方和变成 (a+c)2+(bc)2=a2+b2+2c(ab)+2c2>a2+b2(a + c)^2 + (b - c)^2 = a^2 + b^2 + 2c(a - b) + 2c^2 \gt a^2 + b^2。因此,为了最大化平方和,我们应该让一个数字尽可能大。

时间复杂度 O(n×logM)O(n \times \log M),空间复杂度 O(logM)O(\log M),其中 MM 是数组中的最大值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution:
    def maxSum(self, nums: List[int], k: int) -> int:
        mod = 10**9 + 7
        cnt = [0] * 31
        for x in nums:
            for i in range(31):
                if x >> i & 1:
                    cnt[i] += 1
        ans = 0
        for _ in range(k):
            x = 0
            for i in range(31):
                if cnt[i]:
                    x |= 1 << i
                    cnt[i] -= 1
            ans = (ans + x * x) % mod
        return ans
speed

复杂度分析

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

面试官常问的追问

外企场景
  • question_mark

    Candidates should be able to efficiently identify the most significant elements to modify using bitwise operations.

  • question_mark

    Interviewers should look for the candidate’s ability to optimize the selection of k elements from the array after transformations.

  • question_mark

    The candidate’s approach to the problem should showcase familiarity with bitwise operations and array manipulation for optimization.

warning

常见陷阱

外企场景
  • error

    Failing to optimize the selection of elements after bitwise operations, resulting in suboptimal answers.

  • error

    Overlooking the need for efficient array scanning and hash lookup when selecting the best k elements.

  • error

    Inadequate handling of bitwise operation results, leading to incorrect final values for the selected elements.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Applying bitwise operations but changing the problem constraints (e.g., different k or larger array sizes).

  • arrow_right_alt

    Changing the allowed operations to include XOR or other bitwise manipulations.

  • arrow_right_alt

    Limiting the number of times bitwise operations can be applied, requiring additional optimization strategies.

help

常见问题

外企场景

对数组执行操作使平方和最大题解:数组·哈希·扫描 | LeetCode #2897 困难