1
var largestIsland = function (grid) {4
* Create the merge find (union find) structure --> https://www.youtube.com/watch?v=ibjEGG7ylHk5
* For this case, we will form the sets at once, already assigning the same representative to all members.6
* Thus, we can use this simplistic implementation without path compression and find(i) = mf[i]9
for (let i = 0; i < n * n; i++) {12
// Helper that converts coordinates to array position13
const toPos = (r, c) => r * n + c;14
// Recursively set the merge find structure to represent the islands' map15
const merge = (r, c, repr) => {16
mf[toPos(r, c)] = repr; // Merge coordinate18
* Visit neighbors. To visit a neighboring coordinate we first check for:19
* Boundaries (coordinates stay within matrix limits)20
* Neighbor is land (contains a 1)21
* We didn't visit it already (has different representative)23
if (r > 0 && grid[r - 1][c] === 1 && mf[toPos(r - 1, c)] !== repr) {25
merge(r - 1, c, repr);27
if (c < n - 1 && grid[r][c + 1] === 1 && mf[toPos(r, c + 1)] !== repr) {29
merge(r, c + 1, repr);31
if (r < n - 1 && grid[r + 1][c] === 1 && mf[toPos(r + 1, c)] !== repr) {33
merge(r + 1, c, repr);35
if (c > 0 && grid[r][c - 1] === 1 && mf[toPos(r, c - 1)] !== repr) {37
merge(r, c - 1, repr);41
* For each land (1) position that wasn't already merged into a set,42
* we define a new set with its neighbors.44
for (let i = 0; i < n; i++) {45
for (let j = 0; j < n; j++) {46
const pos = toPos(i, j);47
if (grid[i][j] === 1 && mf[pos] === pos) {53
* We can now calculate the surface of every island by counting the number of cells54
* with the same representative.56
const count = Array(n * n).fill(0);57
mf.forEach((el) => count[el]++);59
* Now we have to decide on which sea (0) should be toggled into land (1).60
* For this we save, for each zero, which sets it is contact with. Once this is done,61
* we can calculate the entire surface by suming 1 + the surface of all touching sets.62
* Again, to add the neighbor set to the store, we need to check that it is a one, that we63
* are within the boundaries, and that it was not already marked as neighbor.64
* We store the maximum surface found, which is the final solution.67
for (let r = 0; r < n; r++) {68
for (let c = 0; c < n; c++) {69
if (grid[r][c] === 0) {71
let currentSurface = 1;74
grid[r - 1][c] === 1 &&75
!touching.includes(mf[toPos(r - 1, c)])78
touching.push(mf[toPos(r - 1, c)]);82
grid[r][c + 1] === 1 &&83
!touching.includes(mf[toPos(r, c + 1)])86
touching.push(mf[toPos(r, c + 1)]);90
grid[r + 1][c] === 1 &&91
!touching.includes(mf[toPos(r + 1, c)])94
touching.push(mf[toPos(r + 1, c)]);98
grid[r][c - 1] === 1 &&99
!touching.includes(mf[toPos(r, c - 1)])102
touching.push(mf[toPos(r, c - 1)]);104
touching.forEach((set) => (currentSurface += count[set]));105
if (currentSurface > maxSurface) {106
maxSurface = currentSurface;112
* If maxSurface remained at zero, it means the input is a matrix of ones.114
if (maxSurface > 0) {