Simple and Best Solution. Beats 💯 in both Runtime and Memory🎉
\n\n\n# Code\ncpp []\nclass Solution {\n    int f[2][1024], a[101];\npublic:\n    int maxScore(vector<vector<int>>& grid) {\n        int n = grid.size(), m = gr
![image.png](https://assets.leetcode.com/users/images/450c88ae-f88e-4cc6-859e-4bd70842b188_1725192551.9762223.png)\n\n\n# Code\n```cpp []\nclass Solution {\n    int f[2][1024], a[101];\npublic:\n    int maxScore(vector<vector<int>>& grid) {\n        int n = grid.size(), m = grid[0].size(), N = 1 << n;\n\n        for (int i = 0; i < n; i++){\n            for (int j = 0; j < m; j++){\n                a[grid[i][j]] |= 1 << i;\n            }\n        }\n\n        for (int i = 1; i <= 100; i++) {\n            copy(f[!(i & 1)], f[!(i & 1)] + N, f[i & 1]);\n\n            for (int j = 0; j < n; j++) {\n                if (a[i] >> j & 1) {\n                    for (int k = 0; k < N; k++) {\n                        if (!(k >> j & 1)) {\n                            f[i & 1][k | 1 << j] = max(f[i & 1][k | 1 << j], f[!(i & 1)][k] + i);\n                        }\n                    }\n                }\n            }\n        }\n\n        return *max_element(f[0], f[0] + N);\n    }\n};\n\n```
['C++']