function numIslands(grid) {
if (!grid || grid.length === 0) return 0;
const rows = grid.length;
const cols = grid[0].length;
let count = 0;
const directions = [[1,0],[-1,0],[0,1],[0,-1]];
function bfs(r, c) {
const queue = [];
queue.push([r, c]);
grid[r][c] = '0';
while (queue.length > 0) {
const [row, col] = queue.shift();
for (const [dr, dc] of directions) {
const nr = row + dr;
const nc = col + dc;
if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && grid[nr][nc] === '1') {
queue.push([nr, nc]);
grid[nr][nc] = '0';
}
}
}
}
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (grid[r][c] === '1') {
bfs(r, c);
count++;
}
}
}
return count;
}
const grid = [
['1','1','0','0','0'],
['1','1','0','0','0'],
['0','0','1','0','0'],
['0','0','0','1','1']
];
console.log(numIslands(grid));The BFS explores each island fully and marks visited cells as '0'. The grid has three separate islands: one big in top-left, one single cell in middle, and one connected pair in bottom-right.
function numIslands(grid) {
if (!grid || grid.length === 0) return 0;
const rows = grid.length;
const cols = grid[0].length;
let count = 0;
function dfs(r, c) {
if (r < 0 || c < 0 || r >= rows || c >= cols || grid[r][c] === '0') return;
grid[r][c] = '0';
dfs(r + 1, c);
dfs(r - 1, c);
dfs(r, c + 1);
dfs(r, c - 1);
}
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (grid[r][c] === '1') {
dfs(r, c);
count++;
}
}
}
return count;
}
const grid = [
['1','1','1','0','0'],
['1','1','0','0','1'],
['0','0','0','1','1'],
['0','0','0','0','0']
];
console.log(numIslands(grid));The DFS marks connected '1's as '0'. The grid has two islands: one large top-left cluster, and one cluster in the middle-right connected via (1,4)-(2,4)-(2,3).
function numIslands(grid) {
if (!grid || grid.length === 0) return 0;
const rows = grid.length;
const cols = grid[0].length;
let count = 0;
const directions = [[1,0],[-1,0],[0,1],[0,-1]];
function bfs(r, c) {
const queue = [];
queue.push([r, c]);
grid[r][c] = '0';
while (queue.length > 0) {
const [row, col] = queue.pop();
for (const [dr, dc] of directions) {
const nr = row + dr;
const nc = col + dc;
if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && grid[nr][nc] === '1') {
queue.push([nr, nc]);
grid[nr][nc] = '0';
}
}
}
}
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (grid[r][c] === '1') {
bfs(r, c);
count++;
}
}
}
return count;
}
const grid = [
['1','1','0'],
['0','1','0'],
['1','0','1']
];
console.log(numIslands(grid));The code runs correctly and outputs 3. queue.pop() implements iterative DFS (LIFO stack) instead of BFS (FIFO queue), but cell marking on enqueue ensures all connected cells are visited correctly regardless of order.
function numIslands(grid) {
if (!grid || grid.length === 0) return 0;
const rows = grid.length;
const cols = grid[0].length;
let count = 0;
const directions = [[1,0],[-1,0],[0,1],[0,-1],[1,1],[1,-1],[-1,1],[-1,-1]];
function dfs(r, c) {
if (r < 0 || c < 0 || r >= rows || c >= cols || grid[r][c] === '0') return;
grid[r][c] = '0';
for (const [dr, dc] of directions) {
dfs(r + dr, c + dc);
}
}
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (grid[r][c] === '1') {
dfs(r, c);
count++;
}
}
}
return count;
}
const grid = [
['1','0','1'],
['0','1','0'],
['1','0','1']
];
console.log(numIslands(grid));Because diagonal neighbors count, all '1's connect into one big island.
Each cell is visited once and marked visited, so total work is proportional to number of cells, O(m*n).
