Select Cells in Grid With Maximum Score
Code\ncpp []\n#include <vector>\n#include <unordered_set>\n#include <algorithm>\n#include <cstring>\n\nclass Solution {\npublic:\n    int dp[1025][101];\n    \n
# Code\n```cpp []\n#include <vector>\n#include <unordered_set>\n#include <algorithm>\n#include <cstring>\n\nclass Solution {\npublic:\n    int dp[1025][101];\n    \n    int maxScore(std::vector<std::vector<int>>& grid) {\n        std::unordered_set<int> uniqueValues;\n        std::vector<int> arr[101];\n\n        for (int i = 0; i < grid.size(); i++) {\n            for (int j = 0; j < grid[0].size(); j++) {\n                uniqueValues.insert(grid[i][j]);\n                arr[grid[i][j]].push_back(i);\n            }\n        }\n\n        std::vector<int> temp(uniqueValues.begin(), uniqueValues.end());\n        std::sort(temp.rbegin(), temp.rend());\n        memset(dp, -1, sizeof(dp));\n\n        return solve(0, 0, temp.size(), temp, arr);\n    }\n\nprivate:\n    int solve(int ix, int mask, int n, const std::vector<int>& temp, const std::vector<int> arr[]) {\n        if (ix == n) {\n            return 0;\n        }\n        if (dp[mask][ix] != -1) return dp[mask][ix];\n\n        int ans = 0;\n\n        for (auto it : arr[temp[ix]]) {\n            if ((mask & (1 << it)) == 0) {\n                ans = std::max(ans, temp[ix] + solve(ix + 1, mask | (1 << it), n, temp, arr));\n            }\n        }\n\n        ans = std::max(ans, solve(ix + 1, mask, n, temp, arr));\n\n        return dp[mask][ix] = ans;\n    }\n};\n\n```
['Array', 'Dynamic Programming', 'Bit Manipulation', 'Matrix', 'Bitmask', 'C++']