LeetCode 题解工作台
统计不开心的朋友
给你一份 n 位朋友的亲近程度列表,其中 n 总是 偶数 。 对每位朋友 i , preferences[i] 包含一份 按亲近程度从高 到低排列 的朋友列表。换句话说,排在列表前面的朋友与 i 的亲近程度比排在列表后面的朋友更高。每个列表中的朋友均以 0 到 n-1 之间的整数表示。 所有的朋友被…
2
题型
6
代码语言
3
相关题
当前训练重点
中等 · 数组·模拟
答案摘要
我们用数组 记录每个朋友与其它朋友的亲近程度,其中 表示朋友 对 的亲近程度(值越小,越亲近),另外,用数组 记录每个朋友的配对朋友。 我们枚举每个朋友 ,对于 的配对朋友 ,我们找到 对 的亲近程度 ,然后枚举比 更亲近的其它朋友 ,如果存在 对 的亲近程度 比 更高,那么 就是不开心的朋友,将结果加一即可。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 数组·模拟 题型思路
题目描述
给你一份 n 位朋友的亲近程度列表,其中 n 总是 偶数 。
对每位朋友 i,preferences[i] 包含一份 按亲近程度从高到低排列 的朋友列表。换句话说,排在列表前面的朋友与 i 的亲近程度比排在列表后面的朋友更高。每个列表中的朋友均以 0 到 n-1 之间的整数表示。
所有的朋友被分成几对,配对情况以列表 pairs 给出,其中 pairs[i] = [xi, yi] 表示 xi 与 yi 配对,且 yi 与 xi 配对。
但是,这样的配对情况可能会使其中部分朋友感到不开心。在 x 与 y 配对且 u 与 v 配对的情况下,如果同时满足下述两个条件,x 就会不开心:
x与u的亲近程度胜过x与y,且u与x的亲近程度胜过u与v
返回 不开心的朋友的数目 。
示例 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 <= 500n是偶数preferences.length == npreferences[i].length == n - 10 <= preferences[i][j] <= n - 1preferences[i]不包含ipreferences[i]中的所有值都是独一无二的pairs.length == n/2pairs[i].length == 2xi != yi0 <= xi, yi <= n - 1- 每位朋友都 恰好 被包含在一对中
解题思路
方法一:枚举
我们用数组 记录每个朋友与其它朋友的亲近程度,其中 表示朋友 对 的亲近程度(值越小,越亲近),另外,用数组 记录每个朋友的配对朋友。
我们枚举每个朋友 ,对于 的配对朋友 ,我们找到 对 的亲近程度 ,然后枚举比 更亲近的其它朋友 ,如果存在 对 的亲近程度 比 更高,那么 就是不开心的朋友,将结果加一即可。
枚举结束后,即可得到不开心的朋友的数目。
时间复杂度 ,空间复杂度 。其中 为朋友的数目。
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
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | 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 |
面试官常问的追问
外企场景- 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.
常见陷阱
外企场景- 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.
进阶变体
外企场景- 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.