zoukankan      html  css  js  c++  java
  • leetcode305- Number of Islands II- hard

    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?

    算法:并查集UnionFind。1.每次遇到加上去的岛屿先把cnt++。2.检查这个岛屿四周是不是也是岛,如果是就Union。3.Union函数里让每次有效Union的时候cnt--即可。

    细节:1.UnionFind的时候是改老大的father,不要只是改自己的father:fathers[find(i)] = find(j); 三个F。2.走四方向的时候记得检查边界。3. 2D展开为1D的时候记得算坐标是x*列数+y!而不是x*行数+y。

    实现1. 写内部类UnionFind:

    class Solution {
    
        private class UnionFind {
            private int[] fathers;
            private int count;
            
            public UnionFind(int capacity) {
                this.fathers = new int[capacity];
                for (int i = 0; i < fathers.length; i++) {
                    fathers[i] = i;
                }
                this.count = 0;
            }
    
            public int find(int x) {
                if (fathers[x] == x) {
                    return x;
                }
                return fathers[x] = find(fathers[x]);
            }
    
            public void union(int i, int j) {
                if (find(i) != find(j)) {
                    // 注意这里要改老大而不是单单j, i
                    fathers[find(j)] = find(i);
                    count--;
                }
            }
    
            public void addCount() {
                count++;
            }
    
            public int getCount(){
                return count;
            }
        }
    
        public List<Integer> numIslands2(int m, int n, int[][] positions) {
            List<Integer> result = new ArrayList<Integer>();
            if (m <= 0 || n <= 0 || positions == null || positions.length == 0 || positions[0].length == 0) {
                return result;
            }
            int[] dx = {-1, 0, 1, 0};
            int[] dy = {0, -1, 0, 1};
            UnionFind uf = new UnionFind(m * n);
            boolean[][] grid = new boolean[m][n];
            for (int i = 0; i < positions.length; i++) {
                int x = positions[i][0];
                int y = positions[i][1];
                grid[x][y] = true;
                uf.addCount();
                for (int j = 0; j < 4; j++) {
                    int newX = x + dx[j];
                    int newY = y + dy[j];
                    // 记得检查边界合格否
                    if (isInBound(newX, newY, m, n) && grid[newX][newY]) {
                        uf.union(flatTo1D(x, y, n), flatTo1D(newX, newY, n));
                    }
                }
                result.add(uf.getCount());
            }
            return result;
        }
    
        private boolean isInBound(int x, int y, int m, int n) {
            return x >= 0 && x < m && y >= 0 && y < n;    
        }
        
        // 算是x*列数+y!!而不是x*行数+y
        private int flatTo1D(int x, int y, int n) {
            return x * n + y;
        }
    
    }

    实现2. 用UnionFind关键函数:

    class Solution {
    
        public List<Integer> numIslands2(int m, int n, int[][] positions) {
            List<Integer> result = new ArrayList<Integer>();
            if (m <= 0 || n <= 0 || positions == null || positions.length == 0 || positions[0].length == 0) {
                return result;
            }
            int[] dx = {-1, 0, 1, 0};
            int[] dy = {0, -1, 0, 1};
            boolean[][] grid = new boolean[m][n];
            int[] parents = new int[m * n];
            int cnt = 0;
    
            for (int i = 0; i < parents.length; i++) {
                parents[i] = i;
            }
            
            for (int i = 0; i < positions.length; i++) {
                int x = positions[i][0];
                int y = positions[i][1];
                grid[x][y] = true;
                cnt++;
                for (int j = 0; j < 4; j++) {
                    int newX = x + dx[j];
                    int newY = y + dy[j];
                    // 记得检查边界合格否
                    if (isInBound(newX, newY, m, n) && grid[newX][newY] 
                        && union(parents, flatTo1D(x, y, n), flatTo1D(newX, newY, n))) {
                        cnt--;
                    }
                }
                result.add(cnt);
            }
            return result;
        }
    
        private int find(int[] parents, int x) {
            if (parents[x] == x) {
                return x;
            }
            return parents[x] = find(parents, parents[x]);
        }
        
        private boolean union(int[] parents, int i, int j) {
            if (find(parents, i) == find(parents, j)) {
                return false;
            }
            parents[find(parents, i)] = find(parents, j);
            return true;
        } 
        
        private boolean isInBound(int x, int y, int m, int n) {
            return x >= 0 && x < m && y >= 0 && y < n;    
        }
        
        // 算是x*列数+y!!而不是x*行数+y
        private int flatTo1D(int x, int y, int n) {
            return x * n + y;
        }
    
    }
  • 相关阅读:
    window.open()弹出窗口防止被禁
    实用Javascript代码片段
    php字符串常用函数
    PHP基本使用
    php开发环境搭建
    what is php?
    Front-end Developer Interview Questions
    jQuery Questions:Front-end Developer Interview Questions
    JS Questions:Front-end Developer Interview Questions
    HTML Questions:Front-end Developer Interview Questions
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/7965654.html
Copyright © 2011-2022 走看看