zoukankan      html  css  js  c++  java
  • 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 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 = 3, positions = [[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?

    Solution:

    the solution is similar to "Number of Connected Components in an Undirected Graph"

     1 public class Solution {
     2     int[][] moves = new int[][]{{1,0},{-1,0},{0,1},{0,-1}};
     3 
     4     public List<Integer> numIslands2(int m, int n, int[][] positions) {
     5         List<Integer> res = new ArrayList<Integer>();
     6         if (positions.length==0) return res;
     7 
     8         int[][] grid = new int[m][n];
     9         List<Integer> rootList = new ArrayList<Integer>();
           // This is IMPORTANT: we need make sure root 1 is on index 1.
    10 rootList.add(0); 11 int index = 1; 12 int count = 0; 13 14 for (int i=0;i<positions.length;i++){ 15 int[] p = positions[i]; 16 for (int j=0;j<4;j++){ 17 int[] next = new int[]{p[0]+moves[j][0],p[1]+moves[j][1]}; 18 if (next[0]>=0 && next[0]<m && next[1]>=0 && next[1]<n && grid[next[0]][next[1]]!=0){ 19 // if p has not assigned index 20 if (grid[p[0]][p[1]]==0){ 21 grid[p[0]][p[1]] = grid[next[0]][next[1]]; 22 } else { 23 // merge root indexs of p and next. 24 int rootP = findRoot(rootList,grid[p[0]][p[1]]); 25 int rootNext = findRoot(rootList,grid[next[0]][next[1]]); 26 if (rootP!=rootNext){ 27 rootList.set(rootNext,rootP); 28 count--; 29 } 30 } 31 } 32 } 33 // if no merge happens, assign a new root index to p 34 if (grid[p[0]][p[1]]==0){ 35 rootList.add(index); 36 grid[p[0]][p[1]] = index++; 37 count++; 38 } 39 40 res.add(count); 41 } 42 43 return res; 44 } 45 46 public int findRoot(List<Integer> rootList, int id){ 47 while (rootList.get(id)!=id){ 48 int root = rootList.get(id); 49 rootList.set(id,rootList.get(root)); 50 id = rootList.get(id); 51 } 52 return id; 53 } 54 55 56 }
  • 相关阅读:
    Silverlight 5 系列学习之一
    WPF中数据绑定问题
    细说ASP.NET Forms身份认证 别人写的不过很透彻就转来了以后用时再看
    再学IHanlder 类----------------关于Asp.net与iis原理网上看博客收获写一个验证码用一般处理程序记的好长时间前就写过不过现在再看有点不一样的感觉
    Oracle常用查看表结构命令
    尝试加载 Oracle 客户端库时引发 BadImageFormatException。如果在安装 32 位 Oracle 客户端组件的情况下以 64 位模式运行,将出现此问题。(遇到了这个问题网上查了下保存下来)
    TxetBox PasswordChar 模式解除
    屏幕抖动一 下
    oracle 日期问题 网上找到自己查阅时方便
    day5-Dns
  • 原文地址:https://www.cnblogs.com/lishiblog/p/5772630.html
Copyright © 2011-2022 走看看