Skip to content

Latest commit

 

History

History

305

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

You are given an empty 2D binary grid grid of size m x n. The grid represents a map where 0's represent water and 1's represent land. Initially, all the cells of grid are water cells (i.e., all the cells are 0's).

We may perform an add land operation which turns the water at position into a land. You are given an array positions where positions[i] = [ri, ci] is the position (ri, ci) at which we should operate the ith operation.

Return an array of integers answer where answer[i] is the number of islands after turning the cell (ri, ci) into a land.

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 1:

Input: m = 3, n = 3, positions = [[0,0],[0,1],[1,2],[2,1]]
Output: [1,1,2,3]
Explanation:
Initially, the 2d grid is filled with water.
- Operation #1: addLand(0, 0) turns the water at grid[0][0] into a land. We have 1 island.
- Operation #2: addLand(0, 1) turns the water at grid[0][1] into a land. We still have 1 island.
- Operation #3: addLand(1, 2) turns the water at grid[1][2] into a land. We have 2 islands.
- Operation #4: addLand(2, 1) turns the water at grid[2][1] into a land. We have 3 islands.

Example 2:

Input: m = 1, n = 1, positions = [[0,0]]
Output: [1]

 

Constraints:

  • 1 <= m, n, positions.length <= 104
  • 1 <= m * n <= 104
  • positions[i].length == 2
  • 0 <= ri < m
  • 0 <= ci < n

 

Follow up: Could you solve it in time complexity O(k log(mn)), where k == positions.length?

Related Topics:
Array, Union Find

Similar Questions:

Solution 1.

// OJ: https://leetcode.com/problems/number-of-islands-ii
// Author: github.com/lzl124631x
// Time: O(P * log(MN))
// Space: O(MN)
class UnionFind {
    vector<int> id;
public:
    UnionFind(int n) : id(n) {
        iota(begin(id), end(id), 0);
    }
    int find(int a) {
        return id[a] == a ? a : (id[a] = find(id[a]));
    }
    void connect(int a, int b) {
        int x = find(a), y = find(b);
        if (x == y) return;
        id[y] = x; // this makes sure that after connecting, the find(a), i.e. x, is kept as the new id of the connected component.
    }
};
class Solution {
public:
    vector<int> numIslands2(int M, int N, vector<vector<int>>& A) {
        vector<vector<int>> G(M, vector<int>(N));
        UnionFind uf(M * N);
        unordered_set<int> s;
        int dirs[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
        auto key = [&](int x, int y) { return x * N + y; };
        vector<int> ans;
        for (auto &p : A) {
            int x = p[0], y = p[1], xy = uf.find(key(x, y));
            G[x][y] = 1;
            for (auto &[dx, dy] : dirs) {
                int a = x + dx, b = y + dy;
                if (a < 0 || a >= M || b < 0 || b >= N || G[a][b] != 1) continue;
                int ab = uf.find(key(a, b));
                s.erase(ab);
                uf.connect(xy, ab);
            }
            s.insert(xy);
            ans.push_back(s.size());
        }
        return ans;
    }
};