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);
            List<Integer> ans = new ArrayList<Integer>();
            for (int[] position : positions) {
                int x = position[0], y = position[1];
                int p = islands.add(x, y);
                for (int[] d : dir) {
                    int q = islands.getID(x + d[0], y + d[1]);
                    if (q > 0 && !islands.find(p, q))
                        islands.unite(p, q);
                }
                ans.add(islands.size());
            }
            return ans;
        }
}
class UnionFind2D {
    private int[] id;
    private int[] sz;
    private int m, n, count;
    public UnionFind2D(int m, int n) {
        this.count = 0;
        this.n = n;
        this.m = m;
        this.id = new int[m * n + 1];
        this.sz = new int[m * n + 1];
    }
    public int index(int x, int y) { return x * n + y + 1; }
    public int size() { return this.count; }
    public int getID(int x, int y) {
        if (0 <= x && x < m && 0<= y && y < n)
            return id[index(x, y)];
        return 0;
    }
    public int add(int x, int y) {
        int i = index(x, y);
        id[i] = i; sz[i] = 1;
        ++count;
        return i;
    }
    public boolean find(int p, int q) {
        return root(p) == root(q);
    }
    public void unite(int p, int q) {
        int i = root(p), j = root(q);
        if (sz[i] < sz[j]) { //weighted quick union
            id[i] = j; sz[j] += sz[i];
        } else {
            id[j] = i; sz[i] += sz[j];
        }
        --count;
    }
    private int root(int i) {
        for (;i != id[i]; i = id[i])
            id[i] = id[id[i]]; //path compression
        return i;
    }
}
时间: 2024-10-27 07:08:40

305.Number of Islands II的相关文章

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

第九篇 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

[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

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] 200. 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 all surrounded by

第八篇 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

200. Number of Islands

https://leetcode.com/problems/number-of-islands/#/description 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. Yo