-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathNumber of Enclaves.js
43 lines (35 loc) · 1.01 KB
/
Number of Enclaves.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
// Runtime: 170 ms (Top 26.04%) | Memory: 54.3 MB (Top 16.57%)
/**
* @param {number[][]} grid
* @return {number}
*/
var numEnclaves = function(grid) {
let count = 0, rowLength = grid.length, colLength = grid[0].length
const updateBoundaryLand = (row,col) => {
if(grid?.[row]?.[col]){
grid[row][col] = 0
updateBoundaryLand(row + 1,col)
updateBoundaryLand(row - 1,col)
updateBoundaryLand(row,col + 1)
updateBoundaryLand(row,col - 1)
}
}
for(let i=0;i<rowLength;i++){
if(grid[i][0] || grid[i][colLength-1]){
updateBoundaryLand(i,0)
updateBoundaryLand(i,colLength-1)
}
}
for(let j=0;j<colLength;j++){
if(grid[0][j] || grid[rowLength-1][j]){
updateBoundaryLand(0,j)
updateBoundaryLand(rowLength-1,j)
}
}
for(let i=0;i<rowLength;i++){
for(let j=0;j<colLength;j++){
if(grid[i][j]) count++
}
}
return count
};