LeetCode 题解工作台

统计不开心的朋友

给你一份 n 位朋友的亲近程度列表,其中 n 总是 偶数 。 对每位朋友 i , preferences[i] 包含一份 按亲近程度从高 到低排列 的朋友列表。换句话说,排在列表前面的朋友与 i 的亲近程度比排在列表后面的朋友更高。每个列表中的朋友均以 0 到 n-1 之间的整数表示。 所有的朋友被…

category

2

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·模拟

bolt

答案摘要

我们用数组 记录每个朋友与其它朋友的亲近程度,其中 表示朋友 对 的亲近程度(值越小,越亲近),另外,用数组 记录每个朋友的配对朋友。 我们枚举每个朋友 ,对于 的配对朋友 ,我们找到 对 的亲近程度 ,然后枚举比 更亲近的其它朋友 ,如果存在 对 的亲近程度 比 更高,那么 就是不开心的朋友,将结果加一即可。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·模拟 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一份 n 位朋友的亲近程度列表,其中 n 总是 偶数

对每位朋友 ipreferences[i] 包含一份 按亲近程度从高到低排列 的朋友列表。换句话说,排在列表前面的朋友与 i 的亲近程度比排在列表后面的朋友更高。每个列表中的朋友均以 0n-1 之间的整数表示。

所有的朋友被分成几对,配对情况以列表 pairs 给出,其中 pairs[i] = [xi, yi] 表示 xiyi 配对,且 yixi 配对。

但是,这样的配对情况可能会使其中部分朋友感到不开心。在 xy 配对且 uv 配对的情况下,如果同时满足下述两个条件,x 就会不开心:

  • xu 的亲近程度胜过 xy,且
  • ux 的亲近程度胜过 uv

返回 不开心的朋友的数目

 

示例 1:

输入:n = 4, preferences = [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], pairs = [[0, 1], [2, 3]]
输出:2
解释:
朋友 1 不开心,因为:
- 1 与 0 配对,但 1 与 3 的亲近程度比 1 与 0 高,且
- 3 与 1 的亲近程度比 3 与 2 高。
朋友 3 不开心,因为:
- 3 与 2 配对,但 3 与 1 的亲近程度比 3 与 2 高,且
- 1 与 3 的亲近程度比 1 与 0 高。
朋友 0 和 2 都是开心的。

示例 2:

输入:n = 2, preferences = [[1], [0]], pairs = [[1, 0]]
输出:0
解释:朋友 0 和 1 都开心。

示例 3:

输入:n = 4, preferences = [[1, 3, 2], [2, 3, 0], [1, 3, 0], [0, 2, 1]], pairs = [[1, 3], [0, 2]]
输出:4

 

提示:

  • 2 <= n <= 500
  • n 是偶数
  • preferences.length == n
  • preferences[i].length == n - 1
  • 0 <= preferences[i][j] <= n - 1
  • preferences[i] 不包含 i
  • preferences[i] 中的所有值都是独一无二的
  • pairs.length == n/2
  • pairs[i].length == 2
  • xi != yi
  • 0 <= xi, yi <= n - 1
  • 每位朋友都 恰好 被包含在一对中
lightbulb

解题思路

方法一:枚举

我们用数组 d\textit{d} 记录每个朋友与其它朋友的亲近程度,其中 d[i][j]\textit{d}[i][j] 表示朋友 iijj 的亲近程度(值越小,越亲近),另外,用数组 p\textit{p} 记录每个朋友的配对朋友。

我们枚举每个朋友 xx,对于 xx 的配对朋友 yy,我们找到 xxyy 的亲近程度 d[x][y]\textit{d}[x][y],然后枚举比 d[x][y]\textit{d}[x][y] 更亲近的其它朋友 uu,如果存在 uuxx 的亲近程度 d[u][x]\textit{d}[u][x]d[u][y]\textit{d}[u][y] 更高,那么 xx 就是不开心的朋友,将结果加一即可。

枚举结束后,即可得到不开心的朋友的数目。

时间复杂度 O(n2)O(n^2),空间复杂度 O(n2)O(n^2)。其中 nn 为朋友的数目。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution:
    def unhappyFriends(
        self, n: int, preferences: List[List[int]], pairs: List[List[int]]
    ) -> int:
        d = [{x: j for j, x in enumerate(p)} for p in preferences]
        p = {}
        for x, y in pairs:
            p[x] = y
            p[y] = x
        ans = 0
        for x in range(n):
            y = p[x]
            for i in range(d[x][y]):
                u = preferences[x][i]
                v = p[u]
                if d[u][x] < d[u][v]:
                    ans += 1
                    break
        return ans
speed

复杂度分析

指标
时间complexity is O(n^2) due to checking each friend against all others using the precomputed rank matrix. Space complexity is O(n^2) for the rank matrix and O(n) for the boolean tracking array.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Asks about handling preference violations efficiently using arrays.

  • question_mark

    Checks if candidate recognizes the need for O(1) preference lookups via a rank matrix.

  • question_mark

    Probes understanding of double counting avoidance in simulation loops.

warning

常见陷阱

外企场景
  • error

    Forgetting to check mutual preference condition when determining unhappiness.

  • error

    Iterating over pairs inefficiently instead of using rank matrix for O(1) comparisons.

  • error

    Double counting unhappy friends by not tracking marked status properly.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Changing pairings dynamically and recomputing unhappy friends.

  • arrow_right_alt

    Adding weighted preferences to compute most unhappy friends.

  • arrow_right_alt

    Extending to n being odd with a single unpaired friend and adjusting rules accordingly.

help

常见问题

外企场景

统计不开心的朋友题解:数组·模拟 | LeetCode #1583 中等