leetcode1583. 统计不开心的朋友
给你一份 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 <= 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
- 每位朋友都 恰好 被包含在一对中
方法:模拟
思路:
这道题还是比较简单的,直接模拟即可完成。
首先使用数组friends[i]
表示对于i分配的朋友是谁,使用二维数组scores[i][j]
表示在i心中,j的亲近程度,这里面的亲近程度我们使用int的形式表示,最亲近的人分数为n-1,最不亲近的人分数为0。
那么,我们看不开心的朋友的定义。
假设一个人p1,给他分配的朋友是p2,那么p1心中,p1和p2的亲近程度为score1=scores[i][j]
,此时,如果存在一个p3,使得p1心中他的亲近程度,大于score1,且p3心中p1的亲近程度,大于p3与给他分配的人的亲近程度,那么p1就是一个不开心的朋友。
转换为公式即**if scores[p1][p3] > score1 and scores[p3][p1] > scores[p3][friends[p3]]
**
对每个人遍历所有的p3,找到符合条件的p3,就res++,break。
最后返回res即可。
代码:
Python3:
class Solution:
def unhappyFriends(self, n: int, preferences: List[List[int]], pairs: List[List[int]]) -> int:
# friends[i]表示给i配对的朋友编号
# scores[i][j]表示在i看来,j的亲近程度分数,分数从高到低为n-1到0,分数越高越亲近
friends = [0]*n
scores = [[0 for _ in range(n)] for _ in range(n)]
# 填写scores和friends
for i in range(n):
for j in range(n-1):
scores[i][preferences[i][j]] = n-1-j
for x,y in pairs:
friends[x] = y
friends[y] = x
res = 0
# 开始遍历所有的人
for i in range(n):
# 这个人设为p1,那么给他分配的是p2,score1表示p1心中,p2的亲近程度
p1 = i
p2 = friends[i]
score1 = scores[p1][p2]
# 按p1心中的亲近程度顺序遍历所有人p3
for p3 in preferences[p1]:
# 如果p1心中p3比p2更亲近,且p2心中p1比分配给p3的人更亲近,那么p1就不开心了,res++
if scores[p1][p3] > score1 and scores[p3][p1] > scores[p3][friends[p3]]:
res += 1
break
return res
cpp:
class Solution {
public:
int unhappyFriends(int n, vector<vector<int>>& preferences, vector<vector<int>>& pairs) {
// friends[i]表示给i配对的朋友编号
// scores[i][j]表示在i看来,j的亲近程度分数,分数从高到低为n-1到0,分数越高越亲近
vector<int>friends(n,0);
vector<vector<int>>scores(n,vector<int>(n,0));
// 填写scores和friends
for (int i = 0; i < n; i++)
for (int j = 0; j < n-1; j++)
scores[i][preferences[i][j]] = n-1-j;
for (auto pair:pairs){
friends[pair[0]] = pair[1];
friends[pair[1]] = pair[0];
}
int res = 0;
// 开始遍历所有的人
for (int i = 0; i < n; i++){
// 这个人设为p1,那么给他分配的是p2,score1表示p1心中,p2的亲近程度
int p1 = i;
int p2 = friends[i];
int score1 = scores[p1][p2];
// 按p1心中的亲近程度顺序遍历所有人p3
for (auto p3 :preferences[p1]){
// 如果p1心中p3比p2更亲近,且p2心中p1比分配给p3的人更亲近,那么p1就不开心了,res++
if (scores[p1][p3] > score1 && scores[p3][p1] > scores[p3][friends[p3]]){
res ++;
break;
}
}
}
return res;
}
};