Video Explanation (Optimising brute force solution step-by-step)
Explanation\n\nClick here for the video\n\n# Code\ncpp []\nint dp[101][1024];\n\nclass Solution {\n    \n    int rows, cols;\n    vector<vector<int>> grid;\n   
# Explanation\n\n[Click here for the video](https://youtu.be/xDiSlVmMFTk)\n\n# Code\n```cpp []\nint dp[101][1024];\n\nclass Solution {\n    \n    int rows, cols;\n    vector<vector<int>> grid;\n    \n    int Score (int val, int row_mask) {\n        if (val > 100) return 0;\n        \n        int &ans = dp[val][row_mask];\n        if (ans != -1) return ans;\n        \n        ans = Score (val+1, row_mask);\n        for (int r = 0; r < rows; r ++) {\n            if (row_mask & (1 << r)) continue;\n            \n            for (int c = 0; c < cols; c ++) {\n                if (grid[r][c] != val) continue;\n                \n                ans = max (ans, val + Score (val+1, row_mask|(1<<r)));\n            }\n        }\n        return ans;\n    }\n    \npublic:\n    int maxScore(vector<vector<int>>& _grid) {\n        memset(dp, -1, sizeof(dp));\n        \n        grid = _grid;\n        rows = grid.size(), cols = grid[0].size();\n        \n        return Score (1, 0);\n    }\n};\n```
['C++']