LeetCode 题解工作台

选择不同 X 值三元组使 Y 值之和最大

给你两个整数数组 x 和 y ,长度均为 n 。你必须选择三个 不同 的下标 i , j 和 k ,满足以下条件: x[i] != x[j] x[j] != x[k] x[k] != x[i] 你的目标是在满足这些条件下 最大化 y[i] + y[j] + y[k] 的值。返回通过选择这样一组三元组…

category

5

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·哈希·扫描

bolt

答案摘要

我们将数组 和 中的元素配对成一个二维数组 ,然后按照 的值从大到小对 进行排序。接下来,我们使用一个哈希表来记录已经选择的 值,并遍历 ,每次选择一个未被选择的 值和对应的 值,直到选择了三个不同的 值为止。 如果在遍历过程中选择了三个不同的 值,则返回这三个 值的和;如果遍历结束后仍未选择三个不同的 值,则返回 -1。

Interview AiBox logo

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

试试 AiBox 面试助手arrow_forward
description

题目描述

给你两个整数数组 xy,长度均为 n。你必须选择三个 不同 的下标 i ,jk,满足以下条件:

  • x[i] != x[j]
  • x[j] != x[k]
  • x[k] != x[i]

你的目标是在满足这些条件下 最大化 y[i] + y[j] + y[k] 的值。返回通过选择这样一组三元组下标所能获得的 最大 可能和。

如果不存在这样的三元组,返回 -1。

 

示例 1:

输入:x = [1,2,1,3,2], y = [5,3,4,6,2]

输出:14

解释:

  • 选择 i = 0x[i] = 1y[i] = 5),j = 1x[j] = 2y[j] = 3),k = 3x[k] = 3y[k] = 6)。
  • 选出的三个 x 中的值互不相同。5 + 3 + 6 = 14 是我们能获得的最大值。因此输出为 14。

示例 2:

输入:x = [1,2,1,2], y = [4,5,6,7]

输出:-1

解释:

  • x 中只有两个不同的值。因此输出为 -1。

 

提示:

  • n == x.length == y.length
  • 3 <= n <= 105
  • 1 <= x[i], y[i] <= 106
lightbulb

解题思路

方法一:排序 + 贪心 + 哈希表

我们将数组 xxyy 中的元素配对成一个二维数组 arr\textit{arr},然后按照 yy 的值从大到小对 arr\textit{arr} 进行排序。接下来,我们使用一个哈希表来记录已经选择的 xx 值,并遍历 arr\textit{arr},每次选择一个未被选择的 xx 值和对应的 yy 值,直到选择了三个不同的 xx 值为止。

如果在遍历过程中选择了三个不同的 xx 值,则返回这三个 yy 值的和;如果遍历结束后仍未选择三个不同的 xx 值,则返回 -1。

时间复杂度 O(n×logn)O(n \times \log n),空间复杂度 O(n)O(n)。其中 nn 为数组 x\textit{x}y\textit{y} 的长度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution:
    def maxSumDistinctTriplet(self, x: List[int], y: List[int]) -> int:
        arr = [(a, b) for a, b in zip(x, y)]
        arr.sort(key=lambda x: -x[1])
        vis = set()
        ans = 0
        for a, b in arr:
            if a in vis:
                continue
            vis.add(a)
            ans += b
            if len(vis) == 3:
                return ans
        return -1
speed

复杂度分析

指标
时间complexity is O(n) for building the hash map and O(k log k) for sorting k unique x-values, where k <= n. Space complexity is O(k) for storing maximum y-values for each unique x in the hash map.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Ask for maximum sum with distinct x-values

  • question_mark

    Check if candidate ignores duplicate x-values when selecting y

  • question_mark

    Look for O(n) hash map preprocessing plus sorting for top three selection

warning

常见陷阱

外企场景
  • error

    Including multiple y-values for the same x, leading to invalid triplets

  • error

    Not handling fewer than three unique x-values, returning incorrect sum

  • error

    Sorting all y-values instead of only the maximums, increasing unnecessary computation

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Maximize sum with four distinct x-values instead of three

  • arrow_right_alt

    Select triplet under a threshold constraint on y-values

  • arrow_right_alt

    Minimize sum by picking distinct x-values instead of maximizing

help

常见问题

外企场景

选择不同 X 值三元组使 Y 值之和最大题解:数组·哈希·扫描 | LeetCode #3572 中等