• 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 }
  • 相关阅读:
    POJ 1981 Circle and Points (扫描线)
    UVA 1514 Piece it together (二分图匹配)
    CodeForces 140D New Year Contest
    HDU 4863 Centroid of a Tree
    HDU 4865 Peter's Hobby
    HDU 4870 Rating
    HDU 4864 Task
    CodeForces 766E Mahmoud and a xor trip
    CodeForces 766D Mahmoud and a Dictionary
    CodeForces 767D Cartons of milk
  • 原文地址:https://www.cnblogs.com/joycelee/p/5318933.html
Copyright © 2020-2023  润新知