|
| 1 | +/** |
| 2 | + * 305. Number of Islands II |
| 3 | + * https://leetcode.com/problems/number-of-islands-ii/ |
| 4 | + * Difficulty: Hard |
| 5 | + * |
| 6 | + * You are given an empty 2D binary grid grid of size m x n. The grid represents a map where 0's |
| 7 | + * represent water and 1's represent land. Initially, all the cells of grid are water cells |
| 8 | + * (i.e., all the cells are 0's). |
| 9 | + * |
| 10 | + * We may perform an add land operation which turns the water at position into a land. You are |
| 11 | + * given an array positions where positions[i] = [ri, ci] is the position (ri, ci) at which we |
| 12 | + * should operate the ith operation. |
| 13 | + * |
| 14 | + * Return an array of integers answer where answer[i] is the number of islands after turning |
| 15 | + * the cell (ri, ci) into a land. |
| 16 | + * |
| 17 | + * An island is surrounded by water and is formed by connecting adjacent lands horizontally |
| 18 | + * or vertically. You may assume all four edges of the grid are all surrounded by water. |
| 19 | + */ |
| 20 | + |
| 21 | +/** |
| 22 | + * @param {number} m |
| 23 | + * @param {number} n |
| 24 | + * @param {number[][]} positions |
| 25 | + * @return {number[]} |
| 26 | + */ |
| 27 | +var numIslands2 = function(m, n, positions) { |
| 28 | + const parent = new Map(); |
| 29 | + const rank = new Map(); |
| 30 | + const result = []; |
| 31 | + const directions = [[-1, 0], [1, 0], [0, -1], [0, 1]]; |
| 32 | + let islandCount = 0; |
| 33 | + |
| 34 | + for (const [row, col] of positions) { |
| 35 | + const current = row * n + col; |
| 36 | + if (parent.has(current)) { |
| 37 | + result.push(islandCount); |
| 38 | + continue; |
| 39 | + } |
| 40 | + islandCount++; |
| 41 | + parent.set(current, current); |
| 42 | + rank.set(current, 0); |
| 43 | + |
| 44 | + for (const [dr, dc] of directions) { |
| 45 | + const newRow = row + dr; |
| 46 | + const newCol = col + dc; |
| 47 | + if (newRow >= 0 && newRow < m && newCol >= 0 && newCol < n) { |
| 48 | + const neighbor = newRow * n + newCol; |
| 49 | + if (parent.has(neighbor)) { |
| 50 | + union(current, neighbor); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + result.push(islandCount); |
| 55 | + } |
| 56 | + |
| 57 | + return result; |
| 58 | + |
| 59 | + function find(node) { |
| 60 | + if (!parent.has(node)) { |
| 61 | + parent.set(node, node); |
| 62 | + rank.set(node, 0); |
| 63 | + } |
| 64 | + if (parent.get(node) !== node) { |
| 65 | + parent.set(node, find(parent.get(node))); |
| 66 | + } |
| 67 | + return parent.get(node); |
| 68 | + } |
| 69 | + |
| 70 | + function union(node1, node2) { |
| 71 | + const root1 = find(node1); |
| 72 | + const root2 = find(node2); |
| 73 | + if (root1 === root2) return; |
| 74 | + const rank1 = rank.get(root1); |
| 75 | + const rank2 = rank.get(root2); |
| 76 | + if (rank1 < rank2) { |
| 77 | + parent.set(root1, root2); |
| 78 | + } else if (rank1 > rank2) { |
| 79 | + parent.set(root2, root1); |
| 80 | + } else { |
| 81 | + parent.set(root2, root1); |
| 82 | + rank.set(root1, rank1 + 1); |
| 83 | + } |
| 84 | + islandCount--; |
| 85 | + } |
| 86 | +}; |
0 commit comments