[LeetCode] Number of Islands II

Problem Description:

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]



This problem requires a classic data structure called UnionFind. Take some efforts to learn it at first, like using this Princeton‘s notes offered by peisi. This note is very nicely written. Take some patinece to read through it :-)

The C++ code is as follows, taking peisi‘s Java code as an example.

 1 class UnionFind2D {
 2 public:
 3     UnionFind2D(int m, int n) {
 4         for (int i = 0; i < m * n + 1; i++) ids.push_back(0);
 5         for (int i = 0; i < m * n + 1; i++) szs.push_back(0);
 6         M = m, N = n, cnt = 0;
 7     }
 8     int index(int x, int y) {
 9         return x * N + y + 1;
10     }
11     int size(void) {
12         return cnt;
13     }
14     int id(int x, int y) {
15         if (x >= 0 && x < M && y >= 0 && y < N)
16             return ids[index(x, y)];
17         return 0;
18     }
19     int add(int x, int y) {
20         int idx = index(x, y);
21         ids[idx] = idx;
22         szs[idx] = 1;
23         cnt++;
24         return idx;
25     }
26     int root(int i) {
27         for (; i != ids[i]; i = ids[i])
28             ids[i] = ids[ids[i]];
29         return i;
30     }
31     bool find(int p, int q) {
32         return root(p) == root(q);
33     }
34     void unite(int p, int q) {
35         int i = root(p), j = root(q);
36         if (szs[i] < szs[j]) {
37             ids[i] = j;
38             szs[j] += szs[i];
39         }
40         else {
41             ids[j] = i;
42             szs[i] += szs[j];
43         }
44         cnt--;
45     }
46 private:
47     vector<int> ids, szs;
48     int M, N, cnt;
49 };
50
51 class Solution {
52 public:
53     vector<int> numIslands2(int m, int n, vector<pair<int, int>>& positions) {
54         UnionFind2D islands(m, n);
55         vector<pair<int, int>> dirs = { { 0, -1 }, { 0, 1 }, { -1, 0 }, { 1, 0 } };
56         vector<int> ans;
57         for (auto& pos : positions) {
58             int x = pos.first, y = pos.second;
59             int p = islands.add(x, y);
60             for (auto& d : dirs) {
61                 int q = islands.id(x + d.first, y + d.second);
62                 if (q && !islands.find(p, q))
63                     islands.unite(p, q);
64             }
65             ans.push_back(islands.size());
66         }
67         return ans;
68     }
69 };
时间: 2024-12-28 15:52:27

[LeetCode] Number of Islands II的相关文章

[LeetCode] 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 o

第九篇 LeetCode Number of Islands II

UnifonFind with path compression and weighting O(klogmn), k is the length of positions array public class Solution { public List<Integer> numIslands2(int m, int n, int[][] positions) { List<Integer> result = new ArrayList<Integer>(); if

305.Number of Islands II

/* * 305.Number of Islands II * 2016-4-3 by Mingyang *Union Find 的题目,直接略过 */ private int[][] dir = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}}; public List<Integer> numIslands2(int m, int n, int[][] positions) { UnionFind2D islands = new UnionFind2D(m, n); L

第八篇 LeetCode Number of Islands

好久没更新了,终于考完了继续回来刷题 岛屿问题属于最基本的DFS, BFS题目 使用DFS时会遇到如果图太大call stack过深的follow up,此时可以转为使用BFS 下一篇Number of Islands II 中将使用另一种Union Find的做法 public class Solution { public int numIslands(char[][] grid) { if (grid == null || grid.length == 0) { return 0; } i

Leetcode 305. Number of Islands II

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

Number of Islands II

Given a n,m which means the row and column of the 2D matrix and an array of pair A( size k). Originally, the 2D matrix is all 0 which means there is only sea in the matrix. The list pair has k operator and each operator has two integer A[i].x, A[i].y

[LeetCode] Number of Islands

Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. 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

LintCode &quot;Number of Islands II&quot;

A typical Union-Find one. I'm using a kinda Union-Find solution here. Some boiler-plate code - yeah I know. class Solution { unordered_set<int> hs; // start from 1 int max_k; public: void dfs(vector<vector<int>> &mt, int x, int y, in

LeetCode Number of Islands 岛的数量(DFS,BFS)

题意:0代表水,1代表陆地,那么被水围起来的就是岛了,给一个01矩阵,问有多少个岛? 思路:DFS还是比较短,实现了一下.如果一个点已经被遍历过了,那就将其置为0就行了,不要去搜0的. 1 class Solution { 2 public: 3 bool isok(vector<vector<char> >& grid,int x,int y) 4 { 5 return x>=0 && y>=0 && x<grid.siz