LeetCode 题解工作台

大餐计数

大餐 是指 恰好包含两道不同餐品 的一餐,其美味程度之和等于 2 的幂。 你可以搭配 任意 两道餐品做一顿大餐。 给你一个整数数组 deliciousness ,其中 deliciousness[i] 是第 i ​​​​​​ ​​​​ ​​​​ 道餐品的美味程度,返回你可以用数组中的餐品做出的不同 …

category

2

题型

code_blocks

4

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·哈希·扫描

bolt

答案摘要

根据题意,我们需要统计数组中两个数的和为 的幂的组合数。直接暴力枚举所有的组合数,时间复杂度为 ,肯定会超时。 我们可以遍历数组,用哈希表 维护数组中每个元素 出现的次数。

Interview AiBox logo

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

试试 AiBox 面试助手arrow_forward
description

题目描述

大餐 是指 恰好包含两道不同餐品 的一餐,其美味程度之和等于 2 的幂。

你可以搭配 任意 两道餐品做一顿大餐。

给你一个整数数组 deliciousness ,其中 deliciousness[i] 是第 i​​​​​​​​​​​​​​ 道餐品的美味程度,返回你可以用数组中的餐品做出的不同 大餐 的数量。结果需要对 109 + 7 取余。

注意,只要餐品下标不同,就可以认为是不同的餐品,即便它们的美味程度相同。

 

示例 1:

输入:deliciousness = [1,3,5,7,9]
输出:4
解释:大餐的美味程度组合为 (1,3) 、(1,7) 、(3,5) 和 (7,9) 。
它们各自的美味程度之和分别为 4 、8 、8 和 16 ,都是 2 的幂。

示例 2:

输入:deliciousness = [1,1,1,3,3,3,7]
输出:15
解释:大餐的美味程度组合为 3 种 (1,1) ,9 种 (1,3) ,和 3 种 (1,7) 。

 

提示:

  • 1 <= deliciousness.length <= 105
  • 0 <= deliciousness[i] <= 220
lightbulb

解题思路

方法一:哈希表 + 枚举二的幂

根据题意,我们需要统计数组中两个数的和为 22 的幂的组合数。直接暴力枚举所有的组合数,时间复杂度为 O(n2)O(n^2) ,肯定会超时。

我们可以遍历数组,用哈希表 cntcnt 维护数组中每个元素 dd 出现的次数。

对于每个元素,我们从小到大枚举二的幂次方 ss 作为两数之和,将哈希表中 sds - d 出现的次数累加到答案中。然后将当前元素 dd 出现的次数加一。

遍历结束后,返回答案即可。

时间复杂度 O(n×logM)O(n\times \log M),其中 nn 是数组 deliciousness 的长度,而 MM 是元素的上限,对于本题,上限 M=220M=2^{20}

我们也可以先用哈希表 cntcnt 统计数组中每个元素出现的次数。

然后从小到大枚举二的幂次方 ss 作为两数之和,对于每个 ss,遍历哈希表每个键值对 (a,m)(a, m),如果 sas - a 也在哈希表中,且 saas - a \neq a,则答案加上 m×cnt[sa]m \times cnt[s - a];如果 sa=as - a = a,则答案加上 m×(m1)m \times (m - 1)

最后,将答案除以 22 之后,模 109+710^9 + 7,返回即可。

时间复杂度与上面的方法相同。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution:
    def countPairs(self, deliciousness: List[int]) -> int:
        mod = 10**9 + 7
        mx = max(deliciousness) << 1
        cnt = Counter()
        ans = 0
        for d in deliciousness:
            s = 1
            while s <= mx:
                ans = (ans + cnt[s - d]) % mod
                s <<= 1
            cnt[d] += 1
        return ans
speed

复杂度分析

指标
时间complexity is O(n), where n is the length of the input array. This is due to the array scan and hash table lookups. The space complexity is O(n) because of the space used by the hash table to store the counts of each deliciousness value.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Candidate should quickly identify that powers of two are a key part of the solution.

  • question_mark

    Look for the ability to use hash tables efficiently to track counts of deliciousness values.

  • question_mark

    Candidates should be able to handle the modulo operation to manage large numbers.

warning

常见陷阱

外企场景
  • error

    Not considering the number of powers of two, leading to unnecessary checks or inefficient algorithms.

  • error

    Failing to handle large results correctly using the modulo operation.

  • error

    Not accounting for duplicates in the array and how they affect the pair counting process.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Consider variations where the deliciousness values are restricted to a smaller range, reducing the number of powers of two to consider.

  • arrow_right_alt

    Incorporate constraints where the array can contain only non-negative values or only positive integers.

  • arrow_right_alt

    Explore a scenario where the array can contain repeated pairs of the same value, requiring careful handling of counting.

help

常见问题

外企场景

大餐计数题解:数组·哈希·扫描 | LeetCode #1711 中等