You are given a list of preferences for n friends, where n is always
even.
For each person i, preferences[i] contains a list of friends sorted in the order of preference. In other words, a friend earlier in the list is more preferred than a friend later in the list. Friends in each list are denoted by integers from 0 to n-1.
All the friends are divided into pairs. The pairings are given in a list
pairs, where pairs[i] = [xi, yi] denotes xi is paired with yi and yi
is paired with xi.
However, this pairing may cause some of the friends to be unhappy. A friend
x is unhappy if x is paired with y and there exists a friend u who is paired with v but:
Input: n =4, preferences =[[1,2,3],[3,2,0],[3,1,0],[1,2,0]], pairs =[[0,1],[2,3]]Output: 2Explanation:
Friend 1is unhappy because:-1is paired with0 but prefers 3 over 0, and
-3 prefers 1 over 2.Friend 3is unhappy because:-3is paired with2 but prefers 1 over 2, and
-1 prefers 3 over 0.Friends 0 and 2 are happy.
A friend is unhappy if they prefer someone else over their current pair, and that someone also prefers them over their own pair. We can use a ranking matrix to quickly check preferences and simulate all possible pairs to find unhappy friends.
classSolution {
public:int unhappyFriends(int n, vector<vector<int>>& preferences, vector<vector<int>>& pairs) {
vector<vector<int>> rank(n, vector<int>(n));
for (int i =0; i < n; ++i)
for (int j =0; j < n; ++j)
rank[i][preferences[i][j]] = j;
vector<int> pair(n);
for (auto& p : pairs) {
pair[p[0]] = p[1];
pair[p[1]] = p[0];
}
int ans =0;
for (int x =0; x < n; ++x) {
int y = pair[x];
for (int i =0; preferences[x][i] != y; ++i) {
int u = preferences[x][i];
int v = pair[u];
if (rank[u][x] < rank[u][v]) {
ans++;
break;
}
}
}
return ans;
}
};
classSolution {
publicintunhappyFriends(int n, int[][] preferences, int[][] pairs) {
int[][] rank =newint[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
rank[i][preferences[i][j]]= j;
int[] pair =newint[n];
for (int[] p : pairs) {
pair[p[0]]= p[1];
pair[p[1]]= p[0];
}
int ans = 0;
for (int x = 0; x < n; x++) {
int y = pair[x];
for (int i = 0; preferences[x][i]!= y; i++) {
int u = preferences[x][i];
int v = pair[u];
if (rank[u][x]< rank[u][v]) {
ans++;
break;
}
}
}
return ans;
}
}
classSolution {
fununhappyFriends(n: Int, preferences: Array<IntArray>, pairs: Array<IntArray>): Int {
val rank = Array(n) { IntArray(n) }
for (i in0 until n)
for (j in0 until n)
rank[i][preferences[i][j]] = j
val pair = IntArray(n)
for (p in pairs) {
pair[p[0]] = p[1]
pair[p[1]] = p[0]
}
var ans = 0for (x in0 until n) {
val y = pair[x]
for (i in0 until n) {
if (preferences[x][i] == y) breakval u = preferences[x][i]
val v = pair[u]
if (rank[u][x] < rank[u][v]) {
ans++break }
}
}
return ans
}
}
classSolution:
defunhappyFriends(self, n: int, preferences: list[list[int]], pairs: list[list[int]]) -> int:
rank = [[0]*n for _ in range(n)]
for i in range(n):
for j, v in enumerate(preferences[i]):
rank[i][v] = j
pair = [0]*n
for x, y in pairs:
pair[x] = y
pair[y] = x
ans =0for x in range(n):
y = pair[x]
for u in preferences[x]:
if u == y:
break v = pair[u]
if rank[u][x] < rank[u][v]:
ans +=1breakreturn ans
impl Solution {
pubfnunhappy_friends(n: i32, preferences: Vec<Vec<i32>>, pairs: Vec<Vec<i32>>) -> i32 {
let n = n asusize;
letmut rank =vec![vec![0; n]; n];
for i in0..n {
for (j, &v) in preferences[i].iter().enumerate() {
rank[i][v asusize] = j;
}
}
letmut pair =vec![0; n];
for p in pairs.iter() {
pair[p[0] asusize] = p[1] asusize;
pair[p[1] asusize] = p[0] asusize;
}
letmut ans =0;
for x in0..n {
let y = pair[x];
for&u in preferences[x].iter() {
if u asusize== y { break; }
let v = pair[u asusize];
if rank[u asusize][x] < rank[u asusize][v] {
ans +=1;
break;
}
}
}
ans
}
}