Skip to content

Commit 0d9d62f

Browse files
committed
added problem 1254
1 parent 844db37 commit 0d9d62f

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package problem1254
2+
3+
/*
4+
Given a 2D grid consists of 0s (land) and 1s (water).
5+
An island is a maximal 4-directionally connected group of 0s and a closed island is an island totally (all left, top, right, bottom) surrounded by 1s.
6+
Return the number of closed islands.
7+
*/
8+
9+
const (
10+
Land = 0
11+
Water = 1
12+
)
13+
14+
func closedIsland(grid [][]int) int {
15+
var numIslands int
16+
var found = make([][]bool, len(grid))
17+
for i := range grid {
18+
found[i] = make([]bool, len(grid[0]))
19+
}
20+
for i := range grid {
21+
for j := range grid[i] {
22+
if grid[i][j] == Land && !found[i][j] {
23+
if !discover(grid, found, i, j) {
24+
numIslands++
25+
}
26+
}
27+
}
28+
}
29+
return numIslands
30+
}
31+
32+
func discover(grid [][]int, found [][]bool, x, y int) bool {
33+
var res bool
34+
if x >= len(grid) || x < 0 || y >= len(grid[0]) || y < 0 {
35+
return false
36+
}
37+
if grid[x][y] == Water || found[x][y] {
38+
return false
39+
}
40+
found[x][y] = true
41+
res = discover(grid, found, x+1, y) || res
42+
res = discover(grid, found, x, y+1) || res
43+
res = discover(grid, found, x-1, y) || res
44+
res = discover(grid, found, x, y-1) || res
45+
return res || isRim(x, y, grid)
46+
}
47+
48+
func isRim(i, j int, grid [][]int) bool {
49+
return i == 0 || j == 0 || i == len(grid)-1 || j == len(grid[0])-1
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package problem1254
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type Result struct {
11+
Input [][]int
12+
Expected int
13+
}
14+
15+
var Results = []Result{
16+
{[][]int{
17+
{1, 1, 1, 1, 1, 1, 1, 0},
18+
{1, 0, 0, 0, 0, 1, 1, 0},
19+
{1, 0, 1, 0, 1, 1, 1, 0},
20+
{1, 0, 0, 0, 0, 1, 0, 1},
21+
{1, 1, 1, 1, 1, 1, 1, 0}}, 2},
22+
{[][]int{
23+
{0, 0, 1, 0, 0},
24+
{0, 1, 0, 1, 0},
25+
{0, 1, 1, 1, 0}}, 1},
26+
{[][]int{
27+
{1, 1, 1, 1, 1, 1, 1},
28+
{1, 0, 0, 0, 0, 0, 1},
29+
{1, 0, 1, 1, 1, 0, 1},
30+
{1, 0, 1, 0, 1, 0, 1},
31+
{1, 0, 1, 1, 1, 0, 1},
32+
{1, 0, 0, 0, 0, 0, 1},
33+
{1, 1, 1, 1, 1, 1, 1}}, 2},
34+
{[][]int{
35+
{0, 0, 1, 1, 0, 1, 0, 0, 1, 0},
36+
{1, 1, 0, 1, 1, 0, 1, 1, 1, 0},
37+
{1, 0, 1, 1, 1, 0, 0, 1, 1, 0},
38+
{0, 1, 1, 0, 0, 0, 0, 1, 0, 1},
39+
{0, 0, 0, 0, 0, 0, 1, 1, 1, 0},
40+
{0, 1, 0, 1, 0, 1, 0, 1, 1, 1},
41+
{1, 0, 1, 0, 1, 1, 0, 0, 0, 1},
42+
{1, 1, 1, 1, 1, 1, 0, 0, 0, 0},
43+
{1, 1, 1, 0, 0, 1, 0, 1, 0, 1},
44+
{1, 1, 1, 0, 1, 1, 0, 1, 1, 0}}, 5},
45+
}
46+
47+
func TestClosedIslands(t *testing.T) {
48+
assert := assert.New(t)
49+
50+
for _, res := range Results {
51+
want := res.Expected
52+
got := closedIsland(res.Input)
53+
assert.Equal(want, got, fmt.Sprintf("%+v", res))
54+
}
55+
}

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ Each problem is in it's own directory, with test files. There are helper package
342342
| 1235 | [Maximum Profit in Job Scheduling](https://leetcode.com/problems/maximum-profit-in-job-scheduling/) | [My Solution](./problems/problem1235) ||
343343
| 1239 | [Maximum Length of a Concatenated String with Unique Characters](https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters) | [My Solution](./problems/problem1239) ||
344344
| 1268 | [Search Suggestions System](https://leetcode.com/problems/search-suggestions-system) | [My Solution](./problems/problem1268) ||
345+
| 1254 | [Number of Closed Islands](https://leetcode.com/problems/number-of-closed-islands) | [My Solution](./problems/problem1254) ||
345346
| 1293 | [Shortest Path in a Grid with Obstacles Elimination](https://leetcode.com/problems/shortest-path-in-a-grid-with-obstacles-elimination) | [My Solution](./problems/problem1293) ||
346347
| 1302 | [Deepest Leaves Sum](https://leetcode.com/problems/deepest-leaves-sum) | [My Solution](./problems/problem1302) ||
347348
| 1305 | [All Elements in Two Binary Search Trees](https://leetcode.com/problems/all-elements-in-two-binary-search-trees) | [My Solution](./problems/problem1305) ||

0 commit comments

Comments
 (0)