Simple Solution Explained in Deep | C++ | Beats 87.33% | DP
\n\n\n# Intuition\nSimilar to graphs we will map elements to the rows they are present in and iterate from max value to lowest while marking the rows they were 
![image.png](https://assets.leetcode.com/users/images/2c2b9fe5-1409-4ef8-b057-b0e35209c90f_1725430255.5268874.png)\n\n\n# Intuition\nSimilar to graphs we will map elements to the rows they are present in and iterate from max value to lowest while marking the rows they were in.\n\n# Approach\nFirst we will have a set of unique elements given in grid and then sort them in descending order.\nAfter this we will map them to their corresponding rows maintaing a vector of them.\n\nWe will use bitmasking to maintain visted rows. It is easy just read comments in code.\n\nFinally apply dp. Thank You\n\n# Complexity\n- Time complexity:\n$$O(2^N)$$\n\n- Space complexity:\n$$O(N*2^N)$$\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int solve(int i,vector<int> &t,unordered_map<int,vector<int>> &mp,int mask,int n,vector<vector<int>> &dp){\n        // Base Case of reaching end of unique elements\n        if (i>=n) return 0;\n\n        if (dp[i][mask]!=-1) return dp[i][mask];\n\n        int ans=0;  \n\n        //Going through rows for a given Unique elements \n        //and seeing if we can choose the element from that row\n        for(auto j:mp[t[i]]){\n            if ((mask & (1<<j)) ==0 ){\n                ans=max(ans,t[i] + solve(i+1,t,mp,(mask | (1<<j)),n,dp));\n            }\n        }\n        //Another option is to leave the current i and choose next ones[]()\n        ans=max(ans,solve(i+1,t,mp,mask,n,dp));\n        return dp[i][mask] = ans;\n    }\n    int maxScore(vector<vector<int>>& grid) {\n        // Set of all Unique Numbers\n        set<int> s;\n        for(int i=0;i<grid.size();i++){\n            for(int j=0;j<grid[0].size();j++){\n                s.insert(grid[i][j]);\n            }\n        }\n        // Sorted Vector in Descending Order\n\n        vector<int> t(s.begin(),s.end());\n        sort(t.begin(),t.end(),greater<int>());\n\n        //Mapping Values to all of their rows\n\n        unordered_map<int,vector<int>> mp;\n        for(int i=0;i<grid.size();i++){\n            for(int j=0;j<grid[0].size();j++){\n                mp[grid[i][j]].push_back(i);\n            }\n        }\n\n        //Initializing DP Matrix\n        //1025 -> 2^10 + 1 . Since max rows are 10. \n        vector<vector<int>> dp(t.size(),vector<int>(1025,-1));\n\n        //Solving the question\n        return solve(0,t,mp,0,t.size(),dp);\n    }\n};\n```\n- See This Video for Reference:\nhttps://www.youtube.com/watch?v=lvUte5NcTJ4\n
['C++']