Skip to content

Commit

Permalink
geeksforgeeks: find the number of islands
Browse files Browse the repository at this point in the history
  • Loading branch information
minizilla committed Dec 10, 2023
1 parent e839d62 commit 018024a
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
13 changes: 13 additions & 0 deletions geeksforgeeks/find-the-number-of-islands/README.md
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).
38 changes: 38 additions & 0 deletions geeksforgeeks/find-the-number-of-islands/solution.go
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)
}
55 changes: 55 additions & 0 deletions geeksforgeeks/find-the-number-of-islands/solution_test.go
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)
})
}
}

0 comments on commit 018024a

Please sign in to comment.