Readable Code (DFS + Memo + BitMask)
Code\npython3 []\nfrom functools import lru_cache\n\nclass Solution:\n    def maxScore(self, grid: List[List[int]]) -> int:\n        m, n = len(grid), len(grid[
# Code\n```python3 []\nfrom functools import lru_cache\n\nclass Solution:\n    def maxScore(self, grid: List[List[int]]) -> int:\n        m, n = len(grid), len(grid[0])\n        \n        @functools.cache\n        def dfs(val, rows_seen_mask):\n            if val > 100:\n                return 0\n\n            best = 0\n            best = max(best, dfs(val + 1, rows_seen_mask))\n\n            for i in range(m):\n                mask = 1 << i # Mask where ith bit is set\n                if rows_seen_mask & mask:  # Check if row i has been seen\n                    continue\n                for j in range(n):\n                    if val != grid[i][j]:\n                        continue\n                    new_mask = rows_seen_mask | mask  # Mark row i as seen\n                    best = max(best, val + dfs(val + 1, new_mask))\n            return best\n\n        return dfs(1, 0)  # Start with all rows unseen (bitmask = 0)\n\n```\n\nHuge credits to the video for making me understand. Very underrated channel -> https://www.youtube.com/watch?v=xDiSlVmMFTk
['Python3']