zoukankan      html  css  js  c++  java
  • 305. 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?

    Similar:

    • 300. Number of Islands
     1 public class Solution {
     2     public List<Integer> numIslands2(int m, int n, int[][] positions) {
     3         List<Integer> cnt = new ArrayList<Integer>();
     4         int[][] root = new int[m][n];
     5         for (int i=0; i<m; i++)
     6             for (int j=0; j<n; j++) {
     7                 root[i][j] = -1;
     8             }
     9         int lastNumber = 0;
    10         
    11         for (int[] pt : positions) {
    12             lastNumber = addLand(root, pt[0], pt[1], lastNumber);
    13             cnt.add(lastNumber);
    14         }
    15         
    16         return cnt;
    17     }
    18     
    19     private int addLand(int[][] root, int r, int c, int num) {
    20         if (root[r][c] != -1) return -1; // used to land this part
    21         int m = root.length;
    22         int n = root[0].length;
    23         
    24         root[r][c] = r * n + c; // root is itself
    25         num += 1;
    26         
    27         if (r-1>=0 && root[r-1][c]!=-1) { // up
    28             if (union(root, r-1, c, r, c))
    29                 num--;
    30         }
    31         if (r+1<m && root[r+1][c]!=-1) { // down
    32             if (union(root, r+1, c, r, c))
    33                 num--;
    34         }
    35         if (c-1>=0 && root[r][c-1]!=-1) { // left
    36             if (union(root, r, c-1, r, c))
    37                 num--;
    38         }
    39         if (c+1<n && root[r][c+1]!=-1) { // right
    40             if (union(root, r, c+1, r, c))
    41                 num--;
    42         }
    43         return num;
    44     }
    45     
    46     private boolean union(int[][] grid, int r1, int c1, int r2, int c2) {
    47         int root1 = find(grid, r1, c1);
    48         int root2 = find(grid, r2, c2);
    49         
    50         if (root1 == root2) return false; // already in one set
    51         
    52         int n = grid[0].length;
    53         int x = root2 / n;
    54         int y = root2 % n;
    55         
    56         grid[x][y] = root1;
    57         return true;
    58     }
    59     
    60     private int find(int[][] grid, int r, int c) {
    61         int n = grid[0].length;
    62         int id = r * n + c;
    63         int rr, cc;
    64         while (id != grid[r][c]) {
    65             rr = grid[r][c] / n;
    66             cc = grid[r][c] % n;
    67             grid[r][c] = grid[rr][cc];
    68             r = rr;
    69             c = cc;
    70             id = rr * n +cc;
    71         }
    72         return id;
    73     }
    74 }
  • 相关阅读:
    html提交表单到Servlet
    Kubernetes(k8s)概念学习、集群安装
    Kubernetes(k8s)入门学习
    Spring Boot整合Scheduled定时任务器、整合Quartz定时任务框架
    Maven项目Run As无Run On Server的解决方法
    SpringBoot异常处理五种方式、Junit单元测试、热部署
    SpringBoot整合整合jsp、整合freemarker、整合Thymeleaf
    SpringBoot整合Servlet、Filter、Listener、访问静态资源、文件上传
    Vue前端路由
    Vue前端交互
  • 原文地址:https://www.cnblogs.com/joycelee/p/5318933.html
Copyright © 2011-2022 走看看