leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1583.cpp (1069B)


0 class Solution { 1 static int position[501][501]; 2 bitset<500> unhappy; 3 4 void is_unhappy(const int x, const int y, const int u, const int v) { 5 if (position[x][u] < position[x][y] && position[u][x] < position[u][v]) { 6 unhappy.set(x), unhappy.set(u); 7 } 8 } 9 10 public: 11 int unhappyFriends(const int n, const vector<vector<int>> &preferences, 12 const vector<vector<int>> &pairs) { 13 for (int i = 0; i < n; i++) { 14 for (int j = 0; j < n - 1; j++) { 15 position[i][preferences[i][j]] = j; 16 } 17 } 18 19 for (int i = 0; i < n / 2; i++) { 20 const int x = pairs[i][0], y = pairs[i][1]; 21 for (int j = i + 1; j < n / 2; j++) { 22 const int u = pairs[j][0], v = pairs[j][1]; 23 is_unhappy(x, y, u, v); 24 is_unhappy(x, y, v, u); 25 is_unhappy(y, x, u, v); 26 is_unhappy(y, x, v, u); 27 } 28 } 29 return unhappy.count(); 30 } 31 }; 32 33 int Solution::position[501][501];