Python3 || recursion, cache,  masks  || T/S: 97% / 59%
Here\'s the intuiton:\n\n1. We manually map the grid to d, a dictionary that maps each value in the grid to a bitmask, which indicates which rows contain that v
Here\'s the intuiton:\n\n1. We manually map the grid to `d`, a dictionary that maps each value in the grid to a bitmask, which indicates which rows contain that value. \n\n1. We use recursion with caching to evaluate possible selections of values from the grid, moving from greater values to lesser values. \n\n1. We employ bitmasks to check which rows are available and to mark them as used when selecting a value in that row. We iterate through each value\'s bitmask and compare it with `cur`, the current selection\'s mask.\n2. Having the dictionary allows us to pass only the row index and a dictionary key (an integer as well) to the recursive function, so we can avoid passing big data structures and thereby using less space and time to recurse. Also, caching helps a little with those concerns as well.\n\n```python3 []\nclass Solution:\n    def maxScore(self, grid: List[List[int]]) -> int:\n\n        @lru_cache(None)\n        def dp(i: int, mask: int)-> int:\n\n            if i == len(values): return 0\n            cur, value = mask, values[i]\n            \n            bits, j, res = d[value], 0, 0\n            \n            for _ in range(bits.bit_length()):\n\n                if (bits%2,cur%2) == (1, 0):\n                    res = max(res, value + dp(i + 1, mask | (1 << j)))\n                j, bits, cur = j + 1, bits//2, cur//2\n                \n            if not res: res = dp(i + 1, mask)\n\n            return res\n        \n\n        d = defaultdict(int)\n\n        for idx, row in enumerate(grid):\n            for num in row: d[num]|= (1<<idx)\n\n        values = sorted(d, reverse = True)\n        return dp(0, 0)\n```\n[https://leetcode.com/problems/select-cells-in-grid-with-maximum-score/submissions/1377067207/](https://leetcode.com/problems/select-cells-in-grid-with-maximum-score/submissions/1377067207/)\n\nI could be wrong (and it\'s even more likely than usual with this problem), but I think that time complexity is *O*(*NK* * 2^*N*)  and space complexity is O*(*K* * 2^*N*), in which *N* ~ `len(grid)` and *K* ~ `number of unique inters in.\n\n\n
['Python3']