Skip to content

Commit 8a8f82a

Browse files
committed
Add solution #305
1 parent 38ea889 commit 8a8f82a

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@
293293
302|[Smallest Rectangle Enclosing Black Pixels](./solutions/0302-smallest-rectangle-enclosing-black-pixels.js)|Hard|
294294
303|[Range Sum Query - Immutable](./solutions/0303-range-sum-query-immutable.js)|Easy|
295295
304|[Range Sum Query 2D - Immutable](./solutions/0304-range-sum-query-2d-immutable.js)|Medium|
296+
305|[Number of Islands II](./solutions/0305-number-of-islands-ii.js)|Hard|
296297
306|[Additive Number](./solutions/0306-additive-number.js)|Medium|
297298
307|[Range Sum Query - Mutable](./solutions/0307-range-sum-query-mutable.js)|Medium|
298299
309|[Best Time to Buy and Sell Stock with Cooldown](./solutions/0309-best-time-to-buy-and-sell-stock-with-cooldown.js)|Medium|
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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

Comments
 (0)