Skip to content

Latest commit

 

History

History
42 lines (35 loc) · 2.33 KB

File metadata and controls

42 lines (35 loc) · 2.33 KB

Count Number of Islands medium #javascript #blind75 #matrix #dfs

by Pawan Kumar @jsartisan

Take the Challenge

Given a 2D grid where:

  • '1' represents land
  • '0' represents water

Count the number of islands. An island is:

  • Formed by connecting adjacent lands horizontally or vertically
  • Surrounded by water
  • Edges of the grid are considered water

Constraints:

  • 1 ≤ grid dimensions ≤ 100
  • grid cells contain only '0' or '1'

Examples:

// Example 1:
const grid1 = [
  ["0","1","1","1","0"],
  ["0","1","0","1","0"],
  ["1","1","0","0","0"],
  ["0","0","0","0","0"]
];
console.log(numIslands(grid1));
// Output: 1
// Explanation: One island connected horizontally and vertically

// Example 2:
const grid2 = [
  ["1","1","0","0","1"],
  ["1","1","0","0","1"],
  ["0","0","1","0","0"],
  ["0","0","0","1","1"]
];
console.log(numIslands(grid2));
// Output: 4
// Explanation: Four separate islands

Back Share your Solutions Check out Solutions