leetcode

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

1947.cpp (877B)


      1 class Solution {
      2   public:
      3     int maxCompatibilitySum(vector<vector<int>> &students, vector<vector<int>> &mentors) {
      4         const int n = students.size(), m = students[0].size();
      5         uint16_t student[9] = {0}, mentor[9] = {0};
      6 
      7         for (uint8_t i = 0; i < n; i++) {
      8             for (const int n : students[i])
      9                 student[i] = (student[i] | n) << 1;
     10             for (const int n : mentors[i])
     11                 mentor[i] = (mentor[i] | n) << 1;
     12         }
     13 
     14         uint8_t res = 0;
     15         vector<uint8_t> idx(n);
     16         iota(begin(idx), end(idx), 0);
     17         do {
     18             uint8_t count = 0;
     19             for (uint8_t i = 0; i < n; i++) {
     20                 count += m - __builtin_popcount(student[i] ^ mentor[idx[i]]);
     21             }
     22             res = max(res, count);
     23         } while (next_permutation(begin(idx), end(idx)));
     24 
     25         return res;
     26     }
     27 };