Swift DFS Solution Similar To Number Of Islands
\nclass Solution {\n    /*\n    \n    Task:\n        1. Perform DFS on each 1 cell and paint it with a unique color >= 2.\n            I. Increase color variabl
```\nclass Solution {\n    /*\n    \n    Task:\n        1. Perform DFS on each 1 cell and paint it with a unique color >= 2.\n            I. Increase color variable for each new 1 cell we visit\n        2. We need a data structure to hold the color and it\'s area, dictionary does it!\n            I. The initial key and value pair is 0 as a cell of 0 has 0 area\n        3. As we finish searching for 1\'s in the grid, there are 2 cases\n            I. Either the grid is filled with only 1\'s, therefore return rows*cols\n            II. Or the grid is all zero\'s, return 1 as we can only flip one 0 cell\n    \n    Complexity:\n        TC: O(R*C) where R = # of rows and C # of columns\n        SC: O(R*C)\n    \n    */\n    \n    var rows = 0\n    var cols = 0\n    var grid: [[Int]] = []\n    let directions: [[Int]] = [[1,0], [-1,0], [0,1], [0,-1]]\n    \n    func largestIsland(_ grid: [[Int]]) -> Int {\n        \n        self.rows = grid.count\n        self.cols = grid[0].count\n        self.grid = grid\n        \n        // Key = color and value is the area. A cell = 0 will have no area, 0.\n        var map: [Int: Int] = [:]\n        map[0] = 0\n        \n        var color = 2\n        \n        // Color All 1\'s\n        for r in 0..<rows {\n            for c in 0..<cols where self.grid[r][c] == 1 {\n                let area = dfs(r,c,color)\n                map[color] = area \n                color += 1\n            }\n        }\n        \n        // The grid could be all 0\'s and no cell was painted\n        var result = map[2, default:0]\n        \n        // Optimization. No need to do extra work as we can flip only 1 cell\n        if result == 0 { return 1 } \n        \n        for r in 0..<rows {\n            for c in 0..<cols where self.grid[r][c] == 0 {\n                \n                // Find colors that are around this cell \n                var uniqueColors: Set<Int> = []\n                \n                for direction in directions {\n                    let nextRow = r + direction[0]\n                    let nextCol = c + direction[1]\n                    \n                    if isValid(nextRow, nextCol) {\n                        let validColor = self.grid[nextRow][nextCol]\n                        uniqueColors.insert(validColor)\n                    }\n                }\n                \n                var totalArea = 1 \n                for color in uniqueColors {\n                    totalArea += map[color]!\n                }\n                result = max(totalArea, result)\n            }\n        }\n        return result\n    }\n    \n    func dfs(_ r: Int, _ c: Int, _ color: Int) -> Int {\n        grid[r][c] = color\n        var totalArea = 1\n        \n        for direction in directions {\n            let nextRow = r + direction[0]\n            let nextCol = c + direction[1]\n            \n            // Only color 1\'s\n            if isValid(nextRow,nextCol) && grid[nextRow][nextCol] == 1 {\n                totalArea += dfs(nextRow,nextCol,color)\n            }\n        }\n        return totalArea\n    }\n    \n    func isValid(_ r: Int, _ c: Int) -> Bool {\n        return r >= 0 && r < rows && c >= 0 && c < cols\n    }\n}\n```
[]