zoukankan      html  css  js  c++  java
  • LeetCode 305. Number of Islands II

    原题链接在这里:https://leetcode.com/problems/number-of-islands-ii/

    题目:

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand operation which turns the water at position (row, col) into a land. Given a list of positions to operate, count the number of islands after each addLand operation. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

    Example:

    Given m = 3, n = 3positions = [[0,0], [0,1], [1,2], [2,1]].
    Initially, the 2d grid grid is filled with water. (Assume 0 represents water and 1 represents land).

    0 0 0
    0 0 0
    0 0 0
    

    Operation #1: addLand(0, 0) turns the water at grid[0][0] into a land.

    1 0 0
    0 0 0   Number of islands = 1
    0 0 0
    

    Operation #2: addLand(0, 1) turns the water at grid[0][1] into a land.

    1 1 0
    0 0 0   Number of islands = 1
    0 0 0
    

    Operation #3: addLand(1, 2) turns the water at grid[1][2] into a land.

    1 1 0
    0 0 1   Number of islands = 2
    0 0 0
    

    Operation #4: addLand(2, 1) turns the water at grid[2][1] into a land.

    1 1 0
    0 0 1   Number of islands = 3
    0 1 0
    

    We return the result as an array: [1, 1, 2, 3]

    Challenge:

    Can you do it in time complexity O(k log mn), where k is the length of the positions?

    题解:

    Union-Find, 一个operation 添加进UnionFind2D型的islands. 看四个方向上islands里parent 对应的值,若是大于0, 就find是不是在一个根下,若不在,就union起来. 四个direction走完,把islands的当前count加入res中.

    为什么Union Find 的parent size 是m*n+1 而不是m*n呢? 因为parent[i] = 0时默认没有parent, 若index从0开始, 那么parent[i] = 0时无法判别是没有parent, 还是parent是0.

    Note: there could be duplicate in the positions.

    Time Complexity: O(k*logmn). k = edges.length. Find: O(logmn).

    Space: O(mn).

    AC Java:

     1 class Solution {
     2     int [] parent;
     3     int [] size;
     4     int count;
     5     
     6     public List<Integer> numIslands2(int m, int n, int[][] positions) {
     7         List<Integer> res = new ArrayList<>();
     8         if(positions == null || positions.length == 0){
     9             return res;
    10         }
    11         
    12         parent = new int[m * n + 1];
    13         size = new int[m * n + 1];
    14         count = 0;
    15         
    16         int [][] dirs = new int[][]{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
    17         
    18         for(int [] p : positions){
    19             int ind1 = p[0] * n + p[1] + 1;
    20             if(parent[ind1] != 0){
    21                 res.add(count);
    22                 continue;
    23             }
    24             
    25             parent[ind1] = ind1;
    26             size[ind1] = 1;
    27             count++;
    28             
    29             for(int [] dir : dirs){
    30                 int x = p[0] + dir[0];
    31                 int y = p[1] + dir[1];
    32                 if(x < 0 || x >= m || y < 0 || y >= n){
    33                     continue;
    34                 }
    35                 
    36                 int ind2 = x * n + y + 1;
    37                 if(parent[ind2] > 0 && !find(ind1, ind2)){
    38                     union(ind1, ind2);
    39                 }  
    40             }
    41             
    42             res.add(count);
    43         }
    44         
    45         return res;
    46     }
    47     
    48     private boolean find(int i, int j){
    49         return root(i) == root(j);
    50     }
    51     
    52     private int root(int i){
    53         while(i != parent[i]){
    54             parent[i] = parent[parent[i]];
    55             i = parent[i];
    56         }
    57         
    58         return parent[i];
    59     }
    60     
    61     private void union(int i, int j){
    62         int p = root(i);
    63         int q = root(j);
    64         if(size[p] > size[q]){
    65             size[p] += size[q];
    66             parent[q] = p;
    67         }else{
    68             size[q] += size[p];
    69             parent[p] = q;
    70         }
    71         
    72         count--;
    73     }
    74 }

    类似Number of IslandsNumber of Connected Components in an Undirected Graph.

  • 相关阅读:
    文件数据源设置
    维护0material主数据,提示Settings for material number conversion not found
    7.5版本COPA数据源创建转换提示“不允许对象名称为空”
    COPA指标自动创建
    IDEA操作数据库
    Docker(快速实战流程)
    Docker(理论部分小结)
    Docker数据卷挂载相关
    解决pycharm启动updating Python interpreter长时间更新
    IDEA完美配置(shell)linux的命令行工具
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/5204431.html
Copyright © 2011-2022 走看看