-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountUnhappyFriends.cpp
More file actions
32 lines (31 loc) · 1010 Bytes
/
countUnhappyFriends.cpp
File metadata and controls
32 lines (31 loc) · 1010 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Source: https://leetcode.com/problems/count-unhappy-friends/
// Author: Miao Zhang
// Date: 2021-05-18
class Solution {
public:
int unhappyFriends(int n, vector<vector<int>>& preferences, vector<vector<int>>& pairs) {
vector<int> pair(n);
for (auto& p: pairs) {
pair[p[0]] = p[1];
pair[p[1]] = p[0];
}
vector<vector<int>> prefer(n, vector<int>(n));
for (int x = 0; x < n; x++) {
for (int i = 0; i < preferences[x].size(); i++) {
prefer[x][preferences[x][i]] = i;
}
}
int res = 0;
for (int x = 0; x < n; x++) {
int y = pair[x];
bool found = false;
for (int u = 0; u < n && !found; u++) {
if (u == x || u == y) continue;
int v = pair[u];
found |= prefer[x][u] < prefer[x][y] && prefer[u][x] < prefer[u][v];
}
if (found) res++;
}
return res;
}
};