Skip to content

Commit 8e53990

Browse files
committed
https://leetcode.com/problems/number-of-islands/
1 parent c198b51 commit 8e53990

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
namespace LeetCodeSolutions.Graph
2+
{
3+
public class NumberOfIslands
4+
{
5+
public int NumIslands(char[][] grid)
6+
{
7+
int islandCount = 0;
8+
int rows = grid.Length;
9+
int cols = grid[0].Length;
10+
11+
// Iterate through each cell in the grid
12+
for (int row = 0; row < rows; row++)
13+
{
14+
for (int col = 0; col < cols; col++)
15+
{
16+
// When we find a land cell ('1'), explore the entire island
17+
if (grid[row][col] == '1')
18+
{
19+
islandCount++;
20+
ExploreIsland(grid, row, col);
21+
}
22+
}
23+
}
24+
25+
return islandCount;
26+
}
27+
28+
private void ExploreIsland(char[][] grid, int row, int col)
29+
{
30+
int rows = grid.Length;
31+
int cols = grid[0].Length;
32+
33+
// Check boundary conditions and if current cell is land
34+
if (row < 0 || col < 0 || row >= rows || col >= cols || grid[row][col] == '0')
35+
{
36+
return;
37+
}
38+
39+
// Mark current cell as visited
40+
grid[row][col] = '0';
41+
42+
// Explore in all four directions (up, right, down, left)
43+
ExploreIsland(grid, row + 1, col); // Down
44+
ExploreIsland(grid, row - 1, col); // Up
45+
ExploreIsland(grid, row, col + 1); // Right
46+
ExploreIsland(grid, row, col - 1); // Left
47+
}
48+
49+
[Test(Description = "https://leetcode.com/problems/number-of-islands/")]
50+
[Category("Medium")]
51+
[Category("LeetCode")]
52+
[Category("Number of Islands")]
53+
[TestCaseSource(nameof(Input))]
54+
[Category("Graph")]
55+
[Category("TopInterview")]
56+
public void Test1((int Output, char[][] Input) item)
57+
{
58+
var response = this.NumIslands(item.Input);
59+
Assert.That(response, Is.EqualTo(item.Output));
60+
}
61+
62+
public static IEnumerable<(int Output, char[][] Input)> Input =>
63+
new List<(int Output, char[][] Input)>()
64+
{
65+
(1, [
66+
['1', '1', '1', '1', '0'],
67+
['1', '1', '0', '1', '0'],
68+
['1', '1', '0', '0', '0'],
69+
['0', '0', '0', '0', '0']
70+
])
71+
};
72+
}
73+
}

LeetCodeSolutions/LeetCodeSolutions.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
<ItemGroup>
1616
<Folder Include="Backtracking\" />
1717
<Folder Include="GraphBfs\" />
18-
<Folder Include="Graph\" />
1918
<Folder Include="Heap\" />
2019
<Folder Include="Intervals\" />
2120
<Folder Include="KadaneAlgorithm\" />

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ The Top Interview 150 collection on Leetcode is a curated set of 150 interview q
143143
| 87 | Kth Smallest Element in a BST | Medium | |
144144
| 98 | Validate Binary Search Tree | Medium ||
145145
| <br> Graph General<br> | | | |
146-
| 89 | Number of Islands | Medium | |
146+
| 89 | Number of Islands | Medium | |
147147
| 90 | Surrounded Regions | Medium | |
148148
| 91 | Clone Graph | Medium | |
149149
| 92 | Evaluate Division | Medium | |

0 commit comments

Comments
 (0)