每日一题——岛屿的最大面积【3.15】

每日一题——岛屿的最大面积【3.15】

题目

  leetcode题目:(链接https://leetcode-cn.com/problems/max-area-of-island)。
  给定一个包含了一些 0 和 1 的非空二维数组 grid 。
  一个 岛屿 是由一些相邻的 1 (代表土地) 构成的组合,这里的「相邻」要求两个 1 必须在水平或者竖直方向上相邻。你可以假设 grid 的四个边缘都被 0(代表水)包围着。
  找到给定的二维数组中最大的岛屿面积。(如果没有岛屿,则返回面积为 0 。)

示例 1:

[[0,0,1,0,0,0,0,1,0,0,0,0,0],
 [0,0,0,0,0,0,0,1,1,1,0,0,0],
 [0,1,1,0,1,0,0,0,0,0,0,0,0],
 [0,1,0,0,1,1,0,0,1,0,1,0,0],
 [0,1,0,0,1,1,0,0,1,1,1,0,0],
 [0,0,0,0,0,0,0,0,0,0,1,0,0],
 [0,0,0,0,0,0,0,1,1,1,0,0,0],
 [0,0,0,0,0,0,0,1,1,0,0,0,0]]

对于上面这个给定矩阵应返回 6。注意答案不应该是 11 ,因为岛屿只能包含水平或垂直的四个方向的 1 。

示例 2:

[[0,0,0,0,0,0,0,0]]

对于上面这个给定的矩阵, 返回 0。

基本思路

  看到这道题我首先想到的就是计算机图形学中的区域填充算法,我们可以把岛屿看作多边形,遍历每个岛屿的过程就类似于将多边形填充的过程。比较好理解的是基于DFS的种子填充算法,较复杂但是快一些的还有扫描线种子填充算法

具体代码

种子填充算法:

//Java
class Solution {
	public int maxAreaOfIsland(int[][] grid) {
		if(grid.length == 0) {
			return 0;
		} 
		
        int max = 0;
		//循环遍历二维数组,直到遇到岛屿
        for(int i = 0; i < grid.length; i++) {   
        	for(int j = 0; j < grid[0].length; j++) {
        		if(grid[i][j] != 0) {
        			//遇到岛屿时,将当前坐标当作种子,执行种子填充算法
        			int temp = cal(new Location(i, j), grid);  
        			if(max < temp) {
            			max = temp;
        			}
        		}
        	}
        }
        return max;
    }
	
	public int cal(Location local, int[][] grid) {
		Stack<Location> s = new Stack<Location>();
		s.push(local);
		int sum = 0;
		
		while(!s.empty()) {
			local = s.pop();
			int i = local.i;
			int j = local.j;
			
			if(grid[i][j] != 0) {
				grid[i][j] = 0;
				sum++;
			}
			//按照左、上、右、下的顺序将为1的土地入栈
			if(j - 1 >= 0 && grid[i][j - 1] == 1) {
				s.push(new Location(i, j - 1));
			}
			if(i - 1 >= 0 && grid[i - 1][j] == 1) {
				s.push(new Location(i - 1, j));
			}
			if(j + 1 < grid[0].length && grid[i][j + 1] == 1) {
				s.push(new Location(i, j + 1));
			}
			if(i + 1 < grid.length && grid[i + 1][j] == 1) {
				s.push(new Location(i + 1, j));
			}
		}
		return sum;
	}
}

class Location {
	public int i;
	public int j;
	
	Location(int i, int j) {
		this.i = i;
		this.j = j;
	}
}

时间复杂度:使用栈的时候时间复杂度计算比较复杂,因为会有点重复入栈的情况,最好情况为 O ( R ∗ C ) O(R*C) O(RC),R、C分别为数组的行、列数。若将栈改为递归运算时间复杂度将缩短为 O ( R ∗ C ) O(R*C) O(RC)
空间复杂度:额外需要空间为R*C的栈,空间复杂度为 O ( R ∗ C ) O(R*C) O(RC),R、C分别为数组的行、列数。

扫描线种子填充算法:

//Java
class Solution {
	public int maxAreaOfIsland(int[][] grid) {
		if(grid.length == 0) {
			return 0;
		} 
		
        int max = 0;
        //循环遍历二维数组,直到遇到岛屿
        for(int i = 0; i < grid.length; i++) {
        	for(int j = 0; j < grid[0].length; j++) {
        		if(grid[i][j] != 0) {
        			//遇到岛屿时,将当前坐标当作种子,执行扫描线种子填充算法
        			int temp = scanLine(new Location(i, j), grid);
        			if(max < temp) {
            			max = temp;
        			}
        		}
        	}
        }
        return max;
    }
	
	public int scanLine(Location local, int[][] grid) {
		int sum = 0;
		Stack<Location> s = new Stack<Location>();
		s.push(local);
		
		while(!s.empty()) {
			local = s.pop();
			int i = local.i;
			int j = local.j;
			
			int left = 0;
			int right = 1;
			//向左扫描
			for(; j - left >= 0 && grid[i][j - left] == 1; left++) {
				sum++;
				grid[i][j - left] = 0;
			}
			//向右扫描
			for(; j + right < grid[0].length && grid[i][j + right] == 1; right++) {
				sum++;
				grid[i][j + right] = 0;
			}
			
			int jLeft = j - left + 1;
			int jRight = j + right - 1;
			
			//查看上方那条横线是否有为1的点
			if(i != 0) {
				searchNew(s, jLeft, jRight, i - 1, grid);
			}		
			//查看下那条横线是否有为1的点
			if(i != grid.length - 1) {
				searchNew(s, jLeft, jRight, i + 1, grid);
			}
		}
		return sum;
	}
	
	public void searchNew(Stack<Location> s, int jLeft, int jRight, int i, int[][] grid) {
		int j = jLeft;
		boolean need = false;
		while(j <= jRight) {
			while(j <= jRight && grid[i][j] == 1) {
				need = true;
				j++;
			}
			if(need) {
				s.push(new Location(i, j - 1));
			}
			while(j <= jRight && grid[i][j] == 0) {
				j++;
			}
		}
	}
}

class Location {
	public int i;
	public int j;
	
	Location(int i, int j) {
		this.i = i;
		this.j = j;
	}
}

  第二种方法较为复杂,没有相关知识储备不建议使用,速度上比第一种快,但是总体上效率一般,最快的方法应该是递归运算的DFS。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值