-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy path1254-number-of-closed-islands.js
104 lines (91 loc) · 2.03 KB
/
1254-number-of-closed-islands.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* @param {number[][]} grid
* @return {number}
*/
const closedIsland = function(grid) {
const m = grid.length, n = grid[0].length
const dirs = [[0,1], [0,-1], [1,0], [-1,0]]
for(let i = 0; i < m; i++) {
for(let j = 0; j < n; j++) {
if((i=== 0 || i === m - 1 || j === 0 || j === n - 1) && grid[i][j] === 0){
fill(i, j)
}
}
}
let res = 0
for(let i = 0; i < m; i++) {
for(let j = 0; j < n; j++) {
if(grid[i][j] === 0) {
res++
fill(i, j)
}
}
}
return res
function fill(i, j) {
if(i < 0 || i >= m || j < 0 || j >= n || grid[i][j] !== 0) return
grid[i][j] = 1
for(const [dx, dy] of dirs) {
const nx = i + dx, ny = j + dy
fill(nx, ny)
}
}
};
// another
/**
* @param {number[][]} grid
* @return {number}
*/
const closedIsland = function(grid) {
const m = grid.length, n = grid[0].length
const arr = []
for(let i = 0; i < m; i++) {
for(let j = 0; j < n; j++) {
if(grid[i][j] === 0) arr.push([i, j])
}
}
const dirs = [[0,1], [0,-1], [1,0], [-1,0]]
let num = 2
for(const [i, j] of arr) {
if(grid[i][j] !== 0) continue
else {
bfs(i, j, num)
num++
}
}
let res = 0
const set = new Set()
for(let i = 2; i < num; i++) {
set.add(i)
}
for(let i = 0; i < m; i++) {
for(let j = 0; j < n; j++) {
if(grid[i][j] > 1 && invalid(i, j)) {
set.delete(grid[i][j])
}
}
}
return set.size
function invalid(i,j) {
if(i === 0 || i === m - 1 || j === 0 || j === n - 1) return true
return false
}
function bfs(i, j, v) {
let q = [[i,j]]
grid[i][j] = v
while(q.length) {
const tmp = []
const size = q.length
for(const [x, y] of q) {
for(const [dx, dy] of dirs) {
const nx = x + dx, ny = y + dy
if(nx >= 0 && nx < m && ny >= 0 && ny < n && grid[nx][ny] === 0) {
grid[nx][ny] = v
tmp.push([nx, ny])
}
}
}
q = tmp
}
}
};