[Swift] Sort descending + DFS
Intuition\nIterate the grid and record all unqiue value and its row index data into an array of turple: [(Int, [Int])], sorted it in desending order, and DFS th
# Intuition\nIterate the grid and record all unqiue `value` and its `row index` data into an array of turple: `[(Int, [Int])]`, sorted it in desending order, and DFS this array, in each DFS operation, iterate all rows that has NOT been visited, and record the max score.\n\nFor example,\nInput:`grid = [[1,2,3],[4,3,2],[1,1,1]]` \nTo array of truple: `[(4, [1]), (3, [0, 1]), (2, [1, 0]), (1, [0, 2])]`\n\n# Complexity\n* Space complexity: `m x n`, where m in number of rows and n is number of columns.\n* Time complexity: `O(u + r + u * log(u))`, where `u` is total unique number, r is total number of rows.\n\n# Code\n```swift []\nclass Solution {\n    func maxScore(_ g: [[Int]]) -> Int {\n        var m: [Int: Set<Int>] = [:], data: [(Int, [Int])] = []\n        for r in 0..<g.count {\n            for c in 0..<g[r].count {\n                m[g[r][c], default: []].insert(r)\n            }\n        }\n        for (k, v) in m { data.append((k, Array(v))) }\n        data.sort { lhs, rhs in lhs.0 > rhs.0 }\n        var path: Set<Int> = [], ch: [[Int]: Int] = [:]\n        return self.dfs(0, data, &path, &ch)!\n    }\n\n    private func dfs(_ i: Int, _ d: [(Int, [Int])], _ p: inout Set<Int>, _ c: inout [[Int]: Int]) -> Int? {\n        guard i < d.count else { return 0 }\n        let key = Array(p).sorted() + [i]\n        if let fnd = c[key] { return fnd }\n        var res: Int?\n        for r in d[i].1 {\n            if p.contains(r) { continue }\n            p.insert(r)\n            if let tmp = self.dfs(i + 1, d, &p, &c) {\n                if let val = res { res = max(val, tmp) }\n                else { res = tmp }\n            }\n            p.remove(r)\n        }\n        if let val = res {\n            res = val + d[i].0\n        } else {\n            res = self.dfs(i + 1, d, &p, &c)\n        }\n        if let res { c[key] = res }\n        return res\n    }\n}\n```
['Swift']