|
| 1 | +#include <bits/stdc++.h> |
| 2 | +#include <gtest/gtest.h> |
| 3 | +using namespace std; |
| 4 | + |
| 5 | + |
| 6 | +//// START |
| 7 | +/* |
| 8 | +## Maximum Compatibility Score Sum |
| 9 | + |
| 10 | +*/ |
| 11 | + |
| 12 | + |
| 13 | +class Solution { |
| 14 | + public: |
| 15 | + int maxCompatibilitySum(vector<vector<int>> &students, vector<vector<int>> &mentors) { |
| 16 | + int len = students.size(); |
| 17 | + string bits; |
| 18 | + bits.resize(len * 2, '0'); |
| 19 | + full.resize(len * 2, '1'); |
| 20 | + return maxCompatibilitySum(bits, students, mentors); |
| 21 | + } |
| 22 | + int maxCompatibilitySum(string &bits, |
| 23 | + const vector<vector<int>> &students, |
| 24 | + const vector<vector<int>> &mentors) { |
| 25 | + { |
| 26 | + const auto &f = memo.find(bits); |
| 27 | + if (f != memo.end())return f->second; |
| 28 | + } |
| 29 | + |
| 30 | + int len = bits.size() / 2; |
| 31 | + if (bits == full) return 0; |
| 32 | + int maxr = 0; |
| 33 | + for (int i = 0; i < len; i++) { |
| 34 | + if (bits[i] == '1')continue; |
| 35 | + |
| 36 | + bits[i] = '1'; |
| 37 | + for (int j = len; j < len * 2; j++) { |
| 38 | + if (bits[j] == '1')continue; |
| 39 | + |
| 40 | + bits[j] = '1'; |
| 41 | + maxr = max(maxr, |
| 42 | + score(students[i], mentors[j - len]) |
| 43 | + + maxCompatibilitySum(bits, students, mentors)); |
| 44 | + bits[j] = '0'; |
| 45 | + } |
| 46 | + bits[i] = '0'; |
| 47 | + } |
| 48 | + memo[bits] = maxr; |
| 49 | + return maxr; |
| 50 | + } |
| 51 | + int score(const vector<int> &v1, const vector<int> &v2) { |
| 52 | + int ret = 0; |
| 53 | + for (int i = 0; i < v1.size(); i++) { |
| 54 | + if (v1[i] == v2[i])ret += 1; |
| 55 | + } |
| 56 | + return ret; |
| 57 | + } |
| 58 | + string full; |
| 59 | + unordered_map<string, int> memo; |
| 60 | +}; |
| 61 | + |
| 62 | +//// END |
| 63 | +struct T { |
| 64 | + |
| 65 | +}; |
| 66 | + |
| 67 | +TEST(Solution, test) { |
| 68 | + T ts[] = { |
| 69 | + { |
| 70 | + |
| 71 | + }, |
| 72 | + { |
| 73 | + |
| 74 | + }, |
| 75 | + |
| 76 | + }; |
| 77 | + |
| 78 | + for (T t : ts) { |
| 79 | + Solution solution; |
| 80 | + |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +int main() { |
| 85 | + testing::InitGoogleTest(); |
| 86 | + |
| 87 | + return RUN_ALL_TESTS(); |
| 88 | +} |
| 89 | + |
| 90 | + |
0 commit comments