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)


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