-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCount_Unhappy_Friends.cpp
34 lines (33 loc) · 1.1 KB
/
Count_Unhappy_Friends.cpp
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
33
34
class Solution {
public:
unordered_map<int,int>m;
bool check(int p,vector<vector<int>>preferences,int partner){
for(int i = 0; i < preferences[p].size() && preferences[p][i] != partner; i++){
int next = m[preferences[p][i]];
int curr = preferences[p][i];
for(int j = 0; j < preferences[curr].size(); j++){
if(preferences[curr][j] == p){
return true;
}
if(preferences[curr][j] == next){
break;
}
}
}
return false;
}
int unhappyFriends(int n, vector<vector<int>>& preferences, vector<vector<int>>& pairs) {
for(int i = 0; i < pairs.size(); i++){
m[pairs[i][0]] = pairs[i][1];
m[pairs[i][1]] = pairs[i][0];
}
int unhappy = 0;
for(int i = 0; i < pairs.size(); i++){
int p1 = pairs[i][0];
int p2 = pairs[i][1];
if(check(p1,preferences,p2)) unhappy++;
if(check(p2,preferences,p1)) unhappy++;
}
return unhappy;
}
};