思路
找到每个陆地的四周的陆地
代码
var maxAreaOfIsland = function(grid) {let rowLen = grid.length, colLen = grid[0].length;let res = 0;for(let i = 0; i < rowLen; i++) {for(let j = 0; j < colLen; j++) {if(grid[i][j] == 1) {res = Math.max(res, dfs(grid, i, j));}}}return res;};function dfs(grid, row, col) {if (row < 0 || row >= grid.length ||col < 0 ||col >= grid[0].length || !grid[row][col]) {return 0}grid[row][col] = 0return 1 + dfs(grid, row+1, col) + dfs(grid, row-1, col) + dfs(grid, row, col - 1) + dfs(grid, row, col + 1)}
复杂度分析
时间复杂度 #card=math&code=O%28Row%20%2A%20Col%29)
空间复杂度 #card=math&code=O%28Row%20%2A%20Col%29)
