leetcode

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

2397.cpp (927B)


      1 class Solution {
      2   public:
      3     int maximumRows(const vector<vector<int>> &matrix, const int numSelect) const {
      4         const int n = size(matrix), m = size(matrix[0]);
      5         unordered_map<uint16_t, int> um;
      6 
      7         for (int i = 0; i < n; i++) {
      8             uint16_t crnt = 0, cnt = 0;
      9             for (int j = 0; j < m; j++) {
     10                 crnt |= matrix[i][j] << j;
     11                 cnt += matrix[i][j];
     12             }
     13             if (cnt > numSelect) continue;
     14             um[crnt]++;
     15         }
     16 
     17         uint16_t res = 0, crnt = (1 << numSelect) - 1;
     18         while (crnt <= 1 << m) {
     19             uint16_t count = 0;
     20             for (const auto [k, v] : um) {
     21                 if ((k & ~crnt) == 0) count += v;
     22             }
     23             res = max(res, count);
     24 
     25             uint16_t t = crnt | (crnt - 1);
     26             crnt = (t + 1) | (((~t & -~t) - 1) >> (__builtin_ctz(crnt) + 1));
     27         }
     28 
     29         return res;
     30     }
     31 };