LeetCode 题解工作台

同积元组

给你一个由 不同 正整数组成的数组 nums ,请你返回满足 a * b = c * d 的元组 (a, b, c, d) 的数量。其中 a 、 b 、 c 和 d 都是 nums 中的元素,且 a != b != c != d 。 示例 1: 输入: nums = [2,3,4,6] 输出: 8 …

category

3

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·哈希·扫描

bolt

答案摘要

假设存在 组数,对于其中任意两组数 $a, b$ 和 $c, d$,均满足 $a \times b = c \times d$ 的条件,则这样的组合一共有 $\mathrm{C}_n^2 = \frac{n \times (n-1)}{2}$ 个。 根据题意每一组满足上述条件的组合可以构成 个满足题意的元组,故将各个相同乘积的组合数乘以 相加(等价于:左移 位)即可得到结果。

Interview AiBox logo

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

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个由 不同 正整数组成的数组 nums ,请你返回满足 a * b = c * d 的元组 (a, b, c, d) 的数量。其中 abcd 都是 nums 中的元素,且 a != b != c != d

 

示例 1:

输入:nums = [2,3,4,6]
输出:8
解释:存在 8 个满足题意的元组:
(2,6,3,4) , (2,6,4,3) , (6,2,3,4) , (6,2,4,3)
(3,4,2,6) , (4,3,2,6) , (3,4,6,2) , (4,3,6,2)

示例 2:

输入:nums = [1,2,4,5,10]
输出:16
解释:存在 16 个满足题意的元组:
(1,10,2,5) , (1,10,5,2) , (10,1,2,5) , (10,1,5,2)
(2,5,1,10) , (2,5,10,1) , (5,2,1,10) , (5,2,10,1)
(2,10,4,5) , (2,10,5,4) , (10,2,4,5) , (10,2,5,4)
(4,5,2,10) , (4,5,10,2) , (5,4,2,10) , (5,4,10,2)

 

提示:

  • 1 <= nums.length <= 1000
  • 1 <= nums[i] <= 104
  • nums 中的所有元素 互不相同
lightbulb

解题思路

方法一:组合数 + 哈希表

假设存在 nn 组数,对于其中任意两组数 a,ba, bc,dc, d,均满足 a×b=c×da \times b = c \times d 的条件,则这样的组合一共有 Cn2=n×(n1)2\mathrm{C}_n^2 = \frac{n \times (n-1)}{2} 个。

根据题意每一组满足上述条件的组合可以构成 88 个满足题意的元组,故将各个相同乘积的组合数乘以 88 相加(等价于:左移 33 位)即可得到结果。

时间复杂度 O(n2)O(n^2),空间复杂度 O(n2)O(n^2)。其中 nn 为数组长度。

1
2
3
4
5
6
7
8
9
class Solution:
    def tupleSameProduct(self, nums: List[int]) -> int:
        cnt = defaultdict(int)
        for i in range(1, len(nums)):
            for j in range(i):
                x = nums[i] * nums[j]
                cnt[x] += 1
        return sum(v * (v - 1) // 2 for v in cnt.values()) << 3
speed

复杂度分析

指标
时间O(n^2)
空间O(n^2)
psychology

面试官常问的追问

外企场景
  • question_mark

    The candidate demonstrates understanding of hashing techniques for counting distinct pairs.

  • question_mark

    The candidate proposes an efficient solution with O(n^2) time complexity, recognizing the inefficiency of brute force.

  • question_mark

    The candidate properly identifies the trade-off between time complexity and space complexity in the solution.

warning

常见陷阱

外企场景
  • error

    Forgetting to handle distinctness of the elements in the pairs, which could lead to incorrect tuple counts.

  • error

    Overlooking the need to use hash maps efficiently, potentially causing unnecessary recomputation.

  • error

    Not considering space complexity when dealing with large input sizes, which may lead to inefficient solutions.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Consider implementing the same problem but allowing repeated elements in the array.

  • arrow_right_alt

    Optimize the solution by using a more advanced data structure, like a balanced binary search tree, instead of a hash map.

  • arrow_right_alt

    Expand the problem to include tuples of more than four elements with the same product.

help

常见问题

外企场景

同积元组题解:数组·哈希·扫描 | LeetCode #1726 中等