Count Unhappy Friends LeetCode solution in C++

1
class Solution {
2
public:
3
int unhappyFriends(int n, vector<vector<int>> &preferences, vector<vector<int>> &pairs) {
4
int unhappy = 0;
5
int v[n];
6
for (int i = 0; i < n / 2; i++) {
7
v[pairs[i][0]] = pairs[i][1];
8
v[pairs[i][1]] = pairs[i][0];
9
}
10
for (int i = 0; i < n; i++) {
11
int j = 0;
12
int pf = preferences[i][j];
13
while (pf != v[i]) {
14
int z = 0;
15
bool f = false;
16
while (preferences[pf][z] != v[pf]) {
17
if (preferences[pf][z] == i) {
18
unhappy++;
19
f = true;
20
break;
21
}
22
z++;
23
}
24
if (f) break;
25
pf = preferences[i][++j];
26
}
27
}
28
return unhappy;
29
}
30
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0