Making A Large Island

In a 2D grid of 0s and 1s, we change at most one 0 to a 1.

After, what is the size of the largest island? (An island is a 4-directionally connected group of 1s).

Example 1:

Input: [[1, 0], [0, 1]]
Output: 3
Explanation: Change one 0 to 1 and connect two 1s, then we get an island with area = 3.

Example 2:

Input: [[1, 1], [1, 0]]
Output: 4
Explanation: Change the 0 to 1 and make the island bigger, only one island with area = 4.

Example 3:

Input: [[1, 1], [1, 1]]
Output: 4
Explanation: Can't change any 0 to 1, only one island with area = 4.

Notes:

  • 1 <= grid.length = grid[0].length <= 50.
  • 0 <= grid[i][j] <= 1.

题目理解:

给定一个只含0,1的矩阵,将所有连接的1成为一个岛屿,1的数目代表岛屿的大小,问,如果最多将一个位置的0改为1,那么能够得到的最大的岛屿面积是多少

解题思路:

大致的思路是,首先遍历整个矩阵,找出所有的岛屿;然后再次遍历矩阵,分析其中所有的0,将0四周的所有岛屿面积相加,找出最大值。在实现方式上,可以使用并查集的方式,将属于一个岛屿的位置映射到一个head上,第二次遍历的时候,只要四周的1不属于同一个岛屿,就将其面积相加,取得最大值即可

class Solution {
	Node[][] root;
	int row, col;
	
	class Node{
		int x, y, count;
		public Node(int a, int b, int c) {
			x = a;
			y = b;
			count = c;
		}
	}
    public int largestIsland(int[][] grid) {
        row = grid.length;
        if(row == 0)
        	return 0;
        col = grid[0].length;
        root = new Node[row][col];
        for(int i = 0; i < row; i++) {
        	for(int j = 0; j < col; j++) {
        		root[i][j] = new Node(i, j, grid[i][j]);
        	}
        }
        int res = 0;
        for(int i = 0; i < row; i++) {
        	for(int j = 0; j < col; j++) {
        		if(grid[i][j] == 0)
        			continue;
        		if(i > 0 && grid[i - 1][j] == 1) {
        			Node root_cur = find(i - 1, j);
        			root_cur.x = i;
        			root_cur.y = j;
        			root[i][j].count += root_cur.count;
        		}
        		if(j > 0 && grid[i][j - 1] == 1) {
        			Node root_cur = find(i, j - 1);
        			if(root_cur.x != i || root_cur.y != j) {
        				root_cur.x = i;
        				root_cur.y = j;
        				root[i][j].count += root_cur.count;
        			}
        		}
        		res = Math.max(res, root[i][j].count);
        	}
        }
        Set<Integer> set = new HashSet<Integer>();
        int sum, pos;
        Node root_cur;
        for(int i = 0; i < row; i++) {
        	for(int j = 0; j < col; j++) {
        		if(grid[i][j] == 1)
        			continue;
        		set.clear();
        		sum = 1;
        		if(i > 0 && grid[i - 1][j] == 1) {
        			root_cur = find(i - 1, j);
        			pos = root_cur.x * col + root_cur.y;
        			sum += root_cur.count;
        			set.add(pos);
        		}
        		if(i + 1 < row && grid[i + 1][j] == 1) {
        			root_cur = find(i + 1, j);
        			pos = root_cur.x * col + root_cur.y;
        			if(!set.contains(pos)) {
        				sum += root_cur.count;
            			set.add(pos);
        			}
        		}
        		if(j - 1 > -1 && grid[i][j - 1] == 1) {
        			root_cur = find(i, j - 1);
        			pos = root_cur.x * col + root_cur.y;
        			if(!set.contains(pos)) {
        				sum += root_cur.count;
            			set.add(pos);
        			}
        		}
        		if(j + 1 < col && grid[i][j + 1] == 1) {
        			root_cur = find(i, j + 1);
        			pos = root_cur.x * col + root_cur.y;
        			if(!set.contains(pos)) {
        				sum += root_cur.count;
            			set.add(pos);
        			}
        		}
        		res = Math.max(res, sum);
        	}
        }
        return res;
    }
    
    public Node find(int x, int y) {
    	Node it = root[x][y];
    	if(x == it.x && y == it.y)
    		return it;
    	return find(it.x, it.y);
    }
}

猜你喜欢

转载自blog.csdn.net/m0_37889928/article/details/82954564