Open azl397985856 opened 2 years ago
dfs
func maxAreaOfIsland(grid [][]int) (ans int) {
m:=len(grid)
n:=len(grid[0])
var dps func(num *int,i,j int)
dps = func(num *int,i,j int) {
*num++
if *num >ans{
ans = *num
}
if i-1>=0&&grid[i-1][j] == 1 {
grid[i-1][j]-=1
dps(num, i-1, j)
}
if j-1>=0&&grid[i][j-1] == 1 {
grid[i][j-1] -=1
dps(num, i, j-1)
}
if i+1<m&&grid[i+1][j] == 1 {
grid[i+1][j] -=1
dps(num, i+1, j)
}
if j+1<n&&grid[i][j+1] == 1 {
grid[i][j+1] -=1
dps(num, i, j+1)
}
}
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if grid[i][j] == 1{
num:=0
grid[i][j]-=1
dps(&num, i, j)
}
}
}
return
}
时间复杂度:O(R×C)。其中 R 是给定网格中的行数,C 是列数。我们访问每个网格最多一次。 空间复杂度:O(R×C),递归的深度最大可能是整个网格的大小,因此最大可能使用O(R×C) 的栈空间。
var maxAreaOfIsland = function(grid) { let x=grid[0].length,y=grid.length; let res=0; function dfs(i,j){ // i 是 y ,j 是 x if(i>=y||j>=x||j<0||i<0||grid[i][j]!=1)return 0; grid[i][j]=-1; return 1+dfs(i+1,j)+dfs(i-1,j)+dfs(i,j-1)+dfs(i,j+1);
}
for(let i=0;i<y;i++){
// i 是 y ,j 是 x
for(let j=0;j<x;j++){
if(grid[i][j]){
res=Math.max(res, dfs(i,j))
}
}
}
return res
};
java code
class Solution {
int ans = 0;
public int maxAreaOfIsland(int[][] grid) {
for(int i = 0;i<grid.length;i++){
for(int j = 0;j<grid[i].length;j++){
if(grid[i][j]==1){
ans = Math.max(dfs(grid,i,j),ans);
}
}
}
return ans;
}
public int dfs(int[][] grid,int i,int j){
if(i<0||j<0||i>=grid.length || j>=grid[i].length || grid[i][j]==0 ){
return 0;
}
grid[i][j]=0;
int count = 1;
count += dfs(grid,i+1,j);
count += dfs(grid,i-1,j);
count += dfs(grid,i,j+1);
count += dfs(grid,i,j-1);
return count;
}
}
时间:O($mn$)
空间:O($mn$)
class Solution {
public int maxAreaOfIsland(int[][] grid) {
int res = 0;
for(int i = 0; i < grid.length; i++){
for(int j = 0; j < grid[0].length; j++){
if(grid[i][j] == 1){
res = Math.max(res, bfs(grid, i, j));
}
}
}
return res;
}
public int bfs(int[][] grid, int i, int j){
int[] dx = {1, -1, 0, 0};
int[] dy = {0, 0, 1, -1};
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[]{i, j});
grid[i][j] = 0;
int area = 1;
while(!queue.isEmpty()){
int[] x = queue.poll();
for(int index = 0; index < 4; index++){
int nx = x[0] + dx[index], ny = x[1] + dy[index];
if(nx>=0 && nx < grid.length && ny >= 0 && ny < grid[0].length && grid[nx][ny] == 1){
grid[nx][ny] = 0;
area += 1;
queue.offer(new int[]{nx, ny});
}
}
}
return area;
}
}
回溯 + 标记:0——水,1——未访问过的陆地,-1——访问过的陆地
class Solution {
public int maxAreaOfIsland(int[][] grid) {
if (grid == null || grid.length == 0 || grid[0].length == 0) {
throw new IllegalArgumentException();
}
int rows = grid.length, cols = grid[0].length;
int areaMax = 0;
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
if (grid[row][col] == 1) {
areaMax = Math.max(areaMax, backtracking(grid, row, col, rows, cols));
}
}
}
return areaMax;
}
// 0-水 1-未访问 -1-已访问
private int backtracking(int[][] grid, int row, int col, int rows, int cols) {
// 越界
if (row < 0 || row >= rows || col < 0 || col >= cols) return 0;
// 水或者已经访问过的陆地
if (grid[row][col] != 1) return 0;
// 当前位置已经访问,置为 -1
grid[row][col] = -1;
return 1 + backtracking(grid, row - 1, col, rows, cols) +
backtracking(grid, row + 1, col, rows, cols) +
backtracking(grid, row, col - 1, rows, cols) +
backtracking(grid, row, col + 1, rows, cols);
}
}
// dfs const dfs = (grid, i, j) => { let m = grid.length, n = grid[0].length; if (i < 0 || j < 0 || i >= m || j >= n) { return 0; } if (grid[i][j] == 0) { return 0; } grid[i][j] = 0; return ( dfs(grid, i + 1, j) + dfs(grid, i - 1, j) + dfs(grid, i, j - 1) + dfs(grid, i, j + 1) + 1 ); };
/**
感觉一般这种从一个点散开的都是用DFS会很直观,而且基本上没什么特殊的优化方法,都需要完全遍历,小trick,记录岛屿的当前大小的时候用一个一个值的数组,相当于变成了引用传递,可以避免定义一堆变量还要修改值
class Solution {
public int maxAreaOfIsland(int[][] grid) {
int ans = 0;
for(int i = 0; i < grid.length; i++) {
for(int j = 0; j < grid[0].length; j++) {
if(grid[i][j] != 0) {
int[] currentArea = new int[1];
dfs(grid, i , j , currentArea); // 这样currentArea就会直接变化,不需要传参了
ans = Math.max(ans, currentArea[0]);
}
}
}
return ans;
}
void dfs(int[][] grid, int row, int col, int[] currentArea) {
int rowMax = grid.length-1, colMax = grid[0].length-1;
if(col < 0 || col > colMax || row < 0 ||row > rowMax || grid[row][col] == 0)
return;
grid[row][col] = 0;
currentArea[0] += 1;
dfs(grid, row, col-1,currentArea);
dfs(grid, row, col+1,currentArea);
dfs(grid,row-1,col,currentArea);
dfs(grid,row+1,col,currentArea);
}
}
BFS
var numIslands = function(grid) {
const rows = grid.length
if (rows === 0) { return 0 }
const cols = grid[0].length
if (cols === 0) { return 0 }
let ans = 0
for(let i = 0;i < rows; i++) {
for(let j = 0; j < cols; j++) {
if (grid[i][j] === '1') {
ans++
const queue = [{ i, j }]
while(queue.length > 0) {
const { i, j } = queue.shift()
if (grid[i][j] === '1') {
grid[i][j] = '0'
}
// right
if (j < cols - 1 && grid[i][j + 1] === '1') {
queue.push({ i, j: j + 1 })
}
// bottom
if (i < rows - 1 && grid[i + 1][j] === '1') {
queue.push({ i: i + 1, j })
}
// top
if (i > 0 && grid[i - 1][j] === '1') {
queue.push({ i: i - 1, j })
}
// left
if (j > 0 && grid[i][j - 1] === '1') {
queue.push({ i, j: j - 1 })
}
}
}
}
}
return ans
};
M为grid的行数, N为grid的列数
class Solution {
public int maxAreaOfIsland(int[][] grid) {
int maxArea = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
maxArea = Math.max(maxArea, dfs(grid,i,j));
}
}
return maxArea;
}
public int dfs (int[][] grid, int i, int j) {
if (i < 0 || j < 0 || i >= grid.length || j >= grid[0].length || grid[i][j] == 0) {
return 0;
}
//Overwrite (i,j) as 0, mark as visited
grid[i][j] = 0;
return 1 + dfs(grid, i + 1, j) + dfs(grid, i, j + 1) + dfs(grid, i - 1, j) + dfs(grid, i, j - 1);
}
}
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: m = len(grid) n = len(grid[0]) count = 0 def dfs(i,j): if i<0 or i>m-1 or j < 0 or j > n-1: return 0 if grid[i][j] == 0 : return 0
grid[i][j] = 0
return 1+dfs(i-1,j)+ dfs(i+1,j) + dfs(i,j-1) + dfs(i,j+1)
for i in range(m):
for j in range(n):
if grid[i][j] == 1:
count =max(count,dfs(i,j))
return count
dfs
class Solution {
public int maxAreaOfIsland(int[][] grid) {
int res=0;
for(int i=0; i<grid.length; i++){
for(int j=0; j<grid[i].length; j++){
if(grid[i][j]==0){
continue;
}
int area=dfs(grid, i, j);
res=Math.max(res, area);
}
}
return res;
}
private int dfs(int[][] grid, int i, int j){
if(i<0 || j<0 || i>=grid.length || j>=grid[0].length){
return 0;
}
if(grid[i][j]==0){
return 0;
}
grid[i][j]=0;
return dfs(grid, i+1, j) +
+ dfs(grid, i-1, j)
+ dfs(grid, i, j+1)
+ dfs(grid, i, j-1)
+ 1;
}
}
Code:
public class Solution {
private int[] xDirections = new int[] {1, 0, -1, 0};
private int[] yDirections = new int[] {0, 1, 0, -1};
private int maxArea = 0;
private int rowNum = 0;
private int colNum = 0;
public int MaxAreaOfIsland(int[,] grid) {
if (grid == null || grid.GetLength(0) == 0)
return maxArea;
rowNum= grid.GetLength(0);
colNum = grid.GetLength(1);
for(int i = 0; i< rowNum; i++)
{
for (int j = 0; j< colNum; j++)
{
if (grid[i, j] == 1)
{
maxArea = Math.Max(maxArea, MaxAreaBFS(grid, i, j));
}
}
}
return maxArea;
}
public int MaxAreaBFS(int[,] grid, int x, int y)
{
int area = 1;
Queue<int> xP = new Queue<int>();
Queue<int> yP = new Queue<int>();
xP.Enqueue(x);
yP.Enqueue(y);
grid[x, y] = 0;
int currentX = 0;
int currentY = 0;
int newX = 0;
int newY = 0;
while(xP.Count > 0)
{
currentX = xP.Dequeue();
currentY = yP.Dequeue();
for (int i = 0; i< 4; i++)
{
newX = currentX + xDirections[i];
newY = currentY + yDirections[i];
if (newX >= 0 && newX < rowNum && newY >= 0 && newY < colNum && grid[newX, newY] == 1)
{
xP.Enqueue(newX);
yP.Enqueue(newY);
grid[newX, newY] = 0;
area++;
}
}
}
return area;
}
}
class Solution { boolean seen[][]; public int maxAreaOfIsland(int[][] grid) { int res = 0; int rows = grid.length; int cols = grid[0].length; seen = new boolean[rows][cols]; for(int i = 0; i < rows; i++) { for(int j = 0; j < cols; j++) { res = Math.max(res, dfs(i, j, grid)); } } return res; }
private int dfs(int row, int col, int[][] grid) {
if(row < 0 || row >= grid.length || col < 0 || col >= grid[row].length || seen[row][col] || grid[row][col] == 0) {
return 0;
}
seen[row][col] = true;
return 1 + dfs(row + 1, col, grid) + dfs(row - 1, col, grid) + dfs(row, col + 1, grid) + dfs(row, col - 1, grid);
}
}
null
入选理由
暂无
题目地址
null
前置知识
暂无
题目描述