-
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.
geeksforgeeks: find the number of islands
- Loading branch information
Showing
3 changed files
with
106 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Find the number of islands | ||
|
||
Given a binary 2D matrix, find the number of islands. | ||
A group of connected 1s (horizontal, vertical, diagonal) forms an island. | ||
|
||
## Copyright Notice | ||
|
||
This problem is based on [content](https://www.geeksforgeeks.org/find-the-number-of-islands-using-dfs/) | ||
from [GeeksforGeeks](https://www.geeksforgeeks.org) | ||
written by GeeksforGeeks | ||
and subject to [GeeksforGeeks copyright](https://www.geeksforgeeks.org/legal/copyright-information/). | ||
The original content from GeeksforGeeks and any modifications made here are attributed to GeeksforGeeks contributors, | ||
and this work is shared under [CC BY-SA 4.0](../LICENSE). |
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,38 @@ | ||
package find_the_number_of_islands | ||
|
||
func Solution(matrix [][]int) int { | ||
if len(matrix) == 0 || len(matrix[0]) == 0 { | ||
return 0 | ||
} | ||
|
||
rows, cols := len(matrix), len(matrix[0]) | ||
var islands int | ||
for r := 0; r < rows; r++ { | ||
for c := 0; c < cols; c++ { | ||
if matrix[r][c] == 1 { | ||
islands++ | ||
dfs(matrix, rows, cols, r, c) | ||
} | ||
} | ||
} | ||
|
||
return islands | ||
} | ||
|
||
func dfs(matrix [][]int, rows, cols, r, c int) { | ||
if r < 0 || r >= rows || | ||
c < 0 || c >= cols || | ||
matrix[r][c] == 0 { | ||
return | ||
} | ||
|
||
matrix[r][c] = 0 | ||
dfs(matrix, rows, cols, r-1, c-1) | ||
dfs(matrix, rows, cols, r-1, c) | ||
dfs(matrix, rows, cols, r-1, c+1) | ||
dfs(matrix, rows, cols, r, c-1) | ||
dfs(matrix, rows, cols, r, c+1) | ||
dfs(matrix, rows, cols, r+1, c-1) | ||
dfs(matrix, rows, cols, r+1, c) | ||
dfs(matrix, rows, cols, r+1, c+1) | ||
} |
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,55 @@ | ||
package find_the_number_of_islands_test | ||
|
||
import ( | ||
"testing" | ||
|
||
sut "github.com/minizilla/minmax/geeksforgeeks/find-the-number-of-islands" | ||
"github.com/minizilla/testr" | ||
) | ||
|
||
func TestFindTheNumberOfIslands(t *testing.T) { | ||
tests := map[string]struct { | ||
matrix [][]int | ||
islands int | ||
}{ | ||
"zero": {nil, 0}, | ||
"one": { | ||
[][]int{ | ||
{0, 1}, | ||
{1, 0}, | ||
}, 1, | ||
}, | ||
"two": { | ||
[][]int{ | ||
{0, 1, 0, 0, 0}, | ||
{1, 0, 0, 1, 1}, | ||
}, 2, | ||
}, | ||
"three": { | ||
[][]int{ | ||
{1, 0, 1, 1}, | ||
{0, 1, 0, 1}, | ||
{0, 0, 0, 0}, | ||
{0, 1, 0, 0}, | ||
{0, 0, 1, 0}, | ||
{1, 0, 0, 0}, | ||
}, 3, | ||
}, | ||
"four": { | ||
[][]int{ | ||
{1, 0, 1, 0, 1, 0, 0, 1, 0}, | ||
{0, 1, 0, 1, 0, 0, 1, 0, 1}, | ||
{0, 0, 0, 0, 0, 0, 0, 1, 0}, | ||
{1, 1, 0, 0, 0, 1, 0, 1, 0}, | ||
}, 4, | ||
}, | ||
} | ||
|
||
for name, tc := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
assert := testr.New(t) | ||
islands := sut.Solution(tc.matrix) | ||
assert.Equal(islands, tc.islands) | ||
}) | ||
} | ||
} |