-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add number of islands solution
- Loading branch information
Showing
4 changed files
with
201 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# [Number of Islands](https://leetcode.com/problems/number-of-islands/) | ||
|
||
## Description | ||
|
||
Given an **m x n** 2D binary grid **grid** which represents a map of **'1'**s (land) and **'0'**s (water), return the number of islands. | ||
|
||
An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water. | ||
|
||
**Example 1:** | ||
|
||
``` | ||
Input: grid = [ | ||
["1","1","1","1","0"], | ||
["1","1","0","1","0"], | ||
["1","1","0","0","0"], | ||
["0","0","0","0","0"] | ||
] | ||
Output: 1 | ||
``` | ||
|
||
**Example 2:** | ||
|
||
``` | ||
Input: grid = [ | ||
["1","1","0","0","0"], | ||
["1","1","0","0","0"], | ||
["0","0","1","0","0"], | ||
["0","0","0","1","1"] | ||
] | ||
Output: 3 | ||
``` | ||
|
||
## Solution | ||
|
||
```typescript | ||
function numIslands(grid: string[][]): number { | ||
if (!grid || grid.length === 0) { | ||
return 0; | ||
} | ||
|
||
const numRows = grid.length; | ||
const numCols = grid[0].length; | ||
let count = 0; | ||
|
||
const directions = [ | ||
[1, 0], | ||
[-1, 0], | ||
[0, 1], | ||
[0, -1], | ||
]; | ||
|
||
function bfs(row: number, col: number) { | ||
const queue: [number, number][] = [[row, col]]; | ||
// Mark the current cell as visited | ||
grid[row][col] = "0"; | ||
|
||
while (queue.length > 0) { | ||
const [currentRow, currentCol] = queue.shift()!; | ||
|
||
for (const [dr, dc] of directions) { | ||
const newRow = currentRow + dr; | ||
const newCol = currentCol + dc; | ||
|
||
if ( | ||
newRow >= 0 && | ||
newRow < numRows && | ||
newCol >= 0 && | ||
newCol < numCols && | ||
grid[newRow][newCol] === "1" | ||
) { | ||
queue.push([newRow, newCol]); | ||
// Mark the adjacent cell as visited | ||
grid[newRow][newCol] = "0"; | ||
} | ||
} | ||
} | ||
} | ||
|
||
for (let row = 0; row < numRows; row++) { | ||
for (let col = 0; col < numCols; col++) { | ||
if (grid[row][col] === "1") { | ||
count++; | ||
bfs(row, col); | ||
} | ||
} | ||
} | ||
|
||
return count; | ||
} | ||
``` | ||
|
||
## Complexity Analysis | ||
|
||
- Time complexity: _O(m \* n)_ | ||
- Space complexity: _O(m \* n)_ | ||
|
||
Where M is the number of rows and N is the number of columns in the grid. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { numIslands } from "./numIslands"; | ||
|
||
describe("Number of Islands", () => { | ||
it("should return the correct number of islands", () => { | ||
const grid1 = [ | ||
["1", "1", "0", "0", "0"], | ||
["1", "1", "0", "0", "0"], | ||
["0", "0", "1", "0", "0"], | ||
["0", "0", "0", "1", "1"], | ||
]; | ||
expect(numIslands(grid1)).toBe(3); | ||
|
||
const grid2 = [ | ||
["1", "1", "1", "1", "0"], | ||
["1", "1", "0", "1", "0"], | ||
["1", "1", "0", "0", "0"], | ||
["0", "0", "0", "0", "0"], | ||
]; | ||
expect(numIslands(grid2)).toBe(1); | ||
}); | ||
|
||
it("should return 0 for an empty grid", () => { | ||
const emptyGrid: string[][] = []; | ||
expect(numIslands(emptyGrid)).toBe(0); | ||
}); | ||
|
||
it("should return 0 when there are no islands", () => { | ||
const noIslandsGrid = [ | ||
["0", "0", "0"], | ||
["0", "0", "0"], | ||
["0", "0", "0"], | ||
]; | ||
expect(numIslands(noIslandsGrid)).toBe(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
export function numIslands(grid: string[][]): number { | ||
if (!grid || grid.length === 0) { | ||
return 0; | ||
} | ||
|
||
const numRows = grid.length; | ||
const numCols = grid[0].length; | ||
let count = 0; | ||
|
||
const directions = [ | ||
[1, 0], | ||
[-1, 0], | ||
[0, 1], | ||
[0, -1], | ||
]; | ||
|
||
function bfs(row: number, col: number) { | ||
const queue: [number, number][] = [[row, col]]; | ||
// Mark the current cell as visited | ||
grid[row][col] = "0"; | ||
|
||
while (queue.length > 0) { | ||
const [currentRow, currentCol] = queue.shift()!; | ||
|
||
for (const [dr, dc] of directions) { | ||
const newRow = currentRow + dr; | ||
const newCol = currentCol + dc; | ||
|
||
if ( | ||
newRow >= 0 && | ||
newRow < numRows && | ||
newCol >= 0 && | ||
newCol < numCols && | ||
grid[newRow][newCol] === "1" | ||
) { | ||
queue.push([newRow, newCol]); | ||
// Mark the adjacent cell as visited | ||
grid[newRow][newCol] = "0"; | ||
} | ||
} | ||
} | ||
} | ||
|
||
for (let row = 0; row < numRows; row++) { | ||
for (let col = 0; col < numCols; col++) { | ||
if (grid[row][col] === "1") { | ||
count++; | ||
bfs(row, col); | ||
} | ||
} | ||
} | ||
|
||
return count; | ||
} |