DP Solution with using bit mask for checking visited row
Intuition\n Describe your first thoughts on how to solve this problem. \nLike value is more, which will increase the time complexity so we go with storing visit
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nLike value is more, which will increase the time complexity so we go with storing visited row.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(R*R*Max_Value*2^R)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n2D dp array\nO(max_value*2^R)\n\n# Code\n```python3 []\n\nclass Solution:\n    def maxScore(self, grid: List[List[int]]) -> int:\n        n, m = len(grid), len(grid[0])\n        dp = [[-1 for _ in range(1024)] for _ in range(101)]\n        def solve(val, is_taken):\n            if val>100 or is_taken&(1<<n):\n                return 0\n            if dp[val][is_taken] != -1:\n                return dp[val][is_taken]\n            ans = 0\n\n            for i in range(n):\n                if ((1<<i) & is_taken)==(1<<i):\n                    continue\n                for j in range(m):\n                    if grid[i][j]==val:\n                        ans = max(ans, grid[i][j]+solve(val+1, is_taken | (1<<i)))\n            else:\n                ans = max(ans, solve(val+1, is_taken))\n            dp[val][is_taken]=ans\n            return ans\n        return solve(0, 0)\n```
['Python3']