leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
1799.cpp (644B)
0 class Solution { 1 int dp[8][16384]; 2 3 public: 4 Solution() { memset(dp, 0xFF, sizeof(dp)); } 5 6 int maxScore(vector<int> &n, int i = 1, int mask = 0) { 7 if (i > n.size() / 2) return 0; 8 if (dp[i][mask] != -1) return dp[i][mask]; 9 dp[i][mask] = 0; 10 for (int j = 0; j < n.size(); j++) { 11 for (auto k = j + 1; k < n.size(); k++) { 12 int new_mask = (1 << j) | (1 << k); 13 if ((mask & new_mask)) continue; 14 dp[i][mask] = max(dp[i][mask], i * gcd(n[j], n[k]) + maxScore(n, i + 1, mask + new_mask)); 15 } 16 } 17 return dp[i][mask]; 18 } 19 };